<!DOCTYPE html><html lang="zh-Hans" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>JAVA | DreamcatcherDENG 的个人博客</title><meta name="author" content="dreamcatcherdeng"><meta name="copyright" content="dreamcatcherdeng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="referrer" content="no-referrer"><meta name="description" content="文档描述">
<meta property="og:type" content="article">
<meta property="og:title" content="JAVA">
<meta property="og:url" content="https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/JAVA/index.html">
<meta property="og:site_name" content="DreamcatcherDENG 的个人博客">
<meta property="og:description" content="文档描述">
<meta property="og:locale">
<meta property="og:image" content="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-72rd8e.jpg">
<meta property="article:published_time" content="2022-07-13T09:38:00.948Z">
<meta property="article:modified_time" content="2022-07-16T16:23:37.815Z">
<meta property="article:author" content="dreamcatcherdeng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-72rd8e.jpg"><link rel="shortcut icon" href="/personal-blog/img/favicon.png"><link rel="canonical" href="https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/JAVA/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/personal-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/personal-blog/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JAVA',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-07-17 00:23:37'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="/personal-blog/atom.xml" title="DreamcatcherDENG 的个人博客" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://n.sinaimg.cn/sinakd10100/452/w1080h972/20200416/93dc-iskepxs3894040.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/personal-blog/archives/"><div class="headline">Articles</div><div class="length-num">34</div></a></div><div class="data-item"><a href="/personal-blog/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a></div><div class="data-item"><a href="/personal-blog/categories/"><div class="headline">Categories</div><div class="length-num">35</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/personal-blog/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-72rd8e.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/personal-blog/">DreamcatcherDENG 的个人博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/personal-blog/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="/personal-blog/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JAVA</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2022-07-13T09:38:00.948Z" title="Created 2022-07-13 17:38:00">2022-07-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2022-07-16T16:23:37.815Z" title="Updated 2022-07-17 00:23:37">2022-07-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/personal-blog/categories/Java/">Java</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">17.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>75min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JAVA"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>文档描述</p>
</blockquote>
 <span id="more"></span>
<h1 id="学习资源"><a href="#学习资源" class="headerlink" title="学习资源"></a>学习资源</h1><p>书籍：</p>
<ul>
<li>《Head First Java》</li>
<li>《Java 8实战》</li>
<li>《Effective Java》</li>
</ul>
<p>视频：</p>
<ul>
<li><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Kb411W75N">java 8</a> </p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV18J411W7cE?from=search&amp;seid=2932013915815479004&amp;spm_id_from=333.337.0.0">java</a></p>
</li>
</ul>
<p>代码：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://github.com/biezhi/30-seconds-of-java8">java 8代码片段</a></li>
</ul>
<p>项目：</p>
<hr>
<hr>
<h1 id="Java简介"><a href="#Java简介" class="headerlink" title="Java简介"></a>Java简介</h1><h2 id="JAVA环境"><a href="#JAVA环境" class="headerlink" title="JAVA环境"></a>JAVA环境</h2><ul>
<li>JRE (Java Runtime Environment)<br>要运行一个java程序只需要 jre 即可</li>
<li>JDK (Java Development Environment)<br>jdk 包含了编译工具javac.exe和运行工具java.exe</li>
</ul>
<p><img src="http://124.221.123.212:8080/images/image-20211017231706372.png" alt="image-20211017231706372"></p>
<h2 id="JAVA语言特点"><a href="#JAVA语言特点" class="headerlink" title="JAVA语言特点"></a>JAVA语言特点</h2><ul>
<li><p>面向对象<br>Java语言的设计集中于对象及其接口 ,它提供了简单的类机制以及动态的接口模型。对象中封装了它的状态变量以及相应的方法 ,实现了模块化和信息隐藏 ;而类则提供了一类对象的原型 ,并且通过继承机制 ,子类可以使用父类所提供的方法 ,实现了代码的复用。</p>
</li>
<li><p>分布性<br>Java是面向网络的语言。通过它提供的类库可以处理 TCP/IP协议 ,用户可以通过URL地址在网络上很方便地访问其它对象。</p>
</li>
<li><p>简单性<br>Java语言是一种面向对象的语言 ,它通过提供最基本的方法来完成指定的任务 ,只需理解一些基本的概念 ,就可以用它编写出适合于各种情况的应用程序。 Java略去了运算符重载、多重继承等模糊的概念 ,并且通过实现自动垃圾收集大大简化了程序设计者的内存管理工作。另外 ,Java也适合于在小型机上运行 ,它的基本解释器及类的支持只有 40KB左右 ,加上标准类库和线程的支持也只有 215KB左右。库和线程的支持也只有 215KB左右。</p>
</li>
<li><p>鲁棒性<br>Java在编译和运行程序时 ,都要对可能出现的问题进行检查 ,以消除错误的产生。它提供自动垃圾收集来进行内存管理 ,防止程序员在管理内存时容易产生的错误。通过集成的面向对象的例外处理机制 ,在编译时,Java提示出可能出现但未被处理的例外 ,帮助程序员正确地进行选择以防止系统的崩溃。另外,Java在编译时还可捕获类型声明中的许多常见错误 ,防止动态运行时不匹配问题的出现。</p>
</li>
<li><p>可移植性<br>与平台无关的特性使 Java程序可以方便地被移植到网络上的不同机器。同时 ,Java的类库中也实现了与不同平台的接口 ,使这些类库可以移植。另外,Java编译器是由 Java语言实现的 ,Java运行时系统由标准 C实现 ,这使得Java系统本身也具有可移植性。</p>
</li>
<li><p>体系结构中立<br>Java解释器生成与体系结构无关的字节码指令 ,只要安装了 Java运行时系统 ,Java程序就可在任意的处理器上运行。这些字节码指令对应于 Java虚拟机中的表示 ,Java解释器得到字节码后 ,对它进行转换 ,使之能够在不同的平台运行。</p>
</li>
<li><p>安全性<br>用于网络、分布环境下的 Java必须要防止病毒的入侵。 Java不支持指针,一切对内存的访问都必须通过对象的实例变量来实现 ,这样就防止程序员使用”特洛伊”木马等欺骗手段访问对象的私有成员 ,同时也避免了指针操作中容易产生的错误。</p>
</li>
<li><p>解释执行<br>Java解释器直接对 Java字节码进行解释执行。字节码本身携带了许多编译时信息 ,使得连接过程更加简单。</p>
</li>
<li><p>动态性<br>Java的设计使它适合于一个不断发展的环境。在类库中可以自由地加入新的方法和实例变量而不会影响用户程序的执行。并且 Java通过接口来支持多重继承 ,使之比严格的类继承具有更灵活的方式和扩展性。</p>
</li>
<li><p>多线程<br>多线程机制使应用程序能够并行执行 ,而且同步机制保证了对共享数据的正确操作。通过使用多线程 ,程序设计者可以分别用不同的线程完成特定的行为 ,而不需要采用全局的事件循环机制 ,这样就很容易地实现网络上的实时交互行为。</p>
</li>
<li><p>高性能<br>和其它解释执行的语言如 BASIC、 TCL不同 ,Java字节码的设计使之能很容易地直接转换成对应于特定CPU的机器码 ,从而得到较高的性能。</p>
</li>
</ul>
<h2 id="JAVA语言发展史"><a href="#JAVA语言发展史" class="headerlink" title="JAVA语言发展史"></a>JAVA语言发展史</h2><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">版本</th>
<th style="text-align:center">年份</th>
<th style="text-align:center">新特性</th>
<th style="text-align:center">类与接口数量</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1.0</td>
<td style="text-align:center">1996</td>
<td style="text-align:center">语言本身</td>
<td style="text-align:center">211</td>
</tr>
<tr>
<td style="text-align:center">1.1</td>
<td style="text-align:center">1997</td>
<td style="text-align:center">内部类</td>
<td style="text-align:center">477</td>
</tr>
<tr>
<td style="text-align:center">1.2</td>
<td style="text-align:center">1998</td>
<td style="text-align:center">无</td>
<td style="text-align:center">1524</td>
</tr>
<tr>
<td style="text-align:center">1.3</td>
<td style="text-align:center">2000</td>
<td style="text-align:center">无</td>
<td style="text-align:center">1840</td>
</tr>
<tr>
<td style="text-align:center">1.4</td>
<td style="text-align:center">2004</td>
<td style="text-align:center">断言</td>
<td style="text-align:center">2723</td>
</tr>
<tr>
<td style="text-align:center">5.0（1.5）</td>
<td style="text-align:center">2004</td>
<td style="text-align:center">泛型、增强for循环、可变元参数、自动打包、元数据、枚举、静态导入</td>
<td style="text-align:center">3279</td>
</tr>
<tr>
<td style="text-align:center">6（1.6）</td>
<td style="text-align:center">2006</td>
<td style="text-align:center">无</td>
<td style="text-align:center">3777</td>
</tr>
</tbody>
</table>
</div>
<blockquote>
<p>java 5.0及以后默认省去了1</p>
</blockquote>
<h2 id="JVM内存模型"><a href="#JVM内存模型" class="headerlink" title="JVM内存模型"></a>JVM内存模型</h2><p><img src="http://124.221.123.212:8080/images/image-20211116154119954.png" alt="image-20211116154119954"></p>
<h1 id="环境搭建"><a href="#环境搭建" class="headerlink" title="环境搭建"></a>环境搭建</h1><h2 id="专业术语"><a href="#专业术语" class="headerlink" title="专业术语"></a>专业术语</h2><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">术语</th>
<th style="text-align:center">缩写</th>
<th style="text-align:center">解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">Java Development Kit</td>
<td style="text-align:center">JDK</td>
<td style="text-align:center">编写Java程序的程序员使用的软件（Java 开发环境工具包）</td>
</tr>
<tr>
<td style="text-align:center">Java Runtime Environment</td>
<td style="text-align:center">JRE</td>
<td style="text-align:center">运行Java程序的用户使用的软件（Java运行环境）</td>
</tr>
<tr>
<td style="text-align:center">Standard Edition</td>
<td style="text-align:center">SE</td>
<td style="text-align:center">用于桌面或简单的服务器应用的Java平台（标准版）</td>
</tr>
<tr>
<td style="text-align:center">Enterprise Edition</td>
<td style="text-align:center">EE</td>
<td style="text-align:center">用于复杂的服务器应用的Java平台（企业版）</td>
</tr>
<tr>
<td style="text-align:center">Micro Edition</td>
<td style="text-align:center">ME</td>
<td style="text-align:center">用于微型手机cell phone和其他小型设备的Java平台（微型版）</td>
</tr>
<tr>
<td style="text-align:center">Java 2</td>
<td style="text-align:center">J2</td>
<td style="text-align:center">一个过时的术语，用于描述1998年~2006年之间的Java版本</td>
</tr>
<tr>
<td style="text-align:center">Software Development Kit</td>
<td style="text-align:center">SDK</td>
<td style="text-align:center">一个过时的术语，用于描述1998年~2006年之间的JDK</td>
</tr>
<tr>
<td style="text-align:center">Update</td>
<td style="text-align:center">u</td>
<td style="text-align:center">Sun的术语，用于发布修改的bug</td>
</tr>
<tr>
<td style="text-align:center">NetBeans</td>
<td style="text-align:center"></td>
<td style="text-align:center">Sun的集成开发环境</td>
</tr>
<tr>
<td style="text-align:center">Java Archive</td>
<td style="text-align:center">JAR</td>
<td style="text-align:center">Java 归档文件,是与平台无关的文件格式(Java标准版归档文件)</td>
</tr>
<tr>
<td style="text-align:center">Web Application Archive</td>
<td style="text-align:center">WAR</td>
<td style="text-align:center">WAR 与 JAR 类似，是JAVA编写的Web应用程序打包文件用于运行在Web容器中</td>
</tr>
<tr>
<td style="text-align:center">Enterprise Archive file</td>
<td style="text-align:center">EAR</td>
<td style="text-align:center">除了包含JAR、WAR以外，还包括 EJB 组件(Java企业版归档文件)</td>
</tr>
<tr>
<td style="text-align:center">Java Virtual Machine</td>
<td style="text-align:center">JVM</td>
<td style="text-align:center">Java虚拟机，用于运行Java字节码</td>
</tr>
</tbody>
</table>
</div>
<h2 id="下载JDK"><a href="#下载JDK" class="headerlink" title="下载JDK"></a>下载JDK</h2><p>登录<a target="_blank" rel="noopener" href="https://www.oracle.com/java/technologies/downloads/">Oracle官方提供的下载页</a>，选择需要下载的版本,这里以<code>java 17</code>为例</p>
<p><img src="http://124.221.123.212:8080/images/image-20220126170554537.png" alt="image-20220126170554537"></p>
<blockquote>
<p>当下载的版本不是最新版时，需要注册Oracle账号进行，后缀为<code>.zip</code>的需要配置环境变量</p>
</blockquote>
<p>选择<code>.exe</code>后缀下载，下载完成双击进入安装向导，选择安装路径，下一步点击下一步即可完成安装</p>
<p>安装完成后在命令提示符里输入<code>java -version</code>，提示版本信息则安装成功</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">C:\<span class="title">Users</span>\****&gt;<span class="title">java</span> -<span class="title">version</span></span></span><br><span class="line"><span class="function"><span class="title">java</span> <span class="title">version</span> &quot;17.0.2&quot; 2022-01-18 <span class="title">LTS</span></span></span><br><span class="line"><span class="function"><span class="title">Java</span>(<span class="title">TM</span>) <span class="title">SE</span> <span class="title">Runtime</span> <span class="title">Environment</span> (<span class="title">build</span> 17.0.2+8-<span class="title">LTS</span>-86)</span></span><br><span class="line"><span class="function"><span class="title">Java</span> <span class="title">HotSpot</span>(<span class="title">TM</span>) 64-<span class="title">Bit</span> <span class="title">Server</span> <span class="title">VM</span> (<span class="title">build</span> 17.0.2+8-<span class="title">LTS</span>-86, <span class="title">mixed</span> <span class="title">mode</span>, <span class="title">sharing</span>)</span></span><br></pre></td></tr></table></figure>
<h2 id="环境变量"><a href="#环境变量" class="headerlink" title="环境变量"></a>环境变量</h2><p>打开资源管理器-&gt;右键单击此电脑-&gt;左键单击属性-&gt;左键单击系统设置-&gt;左键单击环境变量-&gt;单击新建</p>
<p><img src="http://124.221.123.212:8080/images/image-20220126172931132.png" alt="image-20220126172931132"></p>
<p>输入变量名<code>`JAVA_HOME</code>，变量值为<code>java</code>的安装路径，安装路径下可见如下文件</p>
<p><img src="http://124.221.123.212:8080/images/image-20220126173151873.png" alt="image-20220126173151873"></p>
<p><img src="http://124.221.123.212:8080/images/image-20220126173007925.png" alt="image-20220126173007925"></p>
<p>再新建环境变量<code>ClassPath</code>,值为<code>.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;</code></p>
<p><img src="http://124.221.123.212:8080/images/image-20220126173259283.png" alt="image-20220126173259283"></p>
<p>点击所有确认按钮保存成功后，环境变量设置完毕</p>
<h2 id="HelloWorld"><a href="#HelloWorld" class="headerlink" title="HelloWorld"></a>HelloWorld</h2><p>新建文件夹<code>HelloWorld.java</code>,编写如下代码并保存<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorld</span></span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;Hello world!&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>使用如下命令编译</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javac.exe HelloWorld.java</span><br></pre></td></tr></table></figure>
<p>使用如下命令执行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java.exe HelloWorld</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Hello World!</span><br></pre></td></tr></table></figure>
<h2 id="JShell"><a href="#JShell" class="headerlink" title="JShell"></a><code>JShell</code></h2><p>在命令行输入<code>jshell</code>可进入java的交互式编程环境</p>
<p><img src="http://124.221.123.212:8080/images/image-20220310153921327.png" alt="image-20220310153921327"></p>
<h1 id="基础语法"><a href="#基础语法" class="headerlink" title="基础语法"></a>基础语法</h1><h2 id="文档注释"><a href="#文档注释" class="headerlink" title="文档注释"></a>文档注释</h2><ul>
<li><p>单行注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//双斜杠用于单行注释</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>多行注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">用于多行注释</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>类注释：类注释必须放在<code>import</code>之后类之前</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 类注释用于说明该类的特征，使用方法</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>方法注释:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> 变量描述</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 返回值描述</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@throws</span> 类异常描述</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>域注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 域说明</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>通用注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@deprecated</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@see</span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> </span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="标识符"><a href="#标识符" class="headerlink" title="标识符"></a>标识符</h2><p>在java语言中，变量、常量、类、对象、方法、语句块等的名字被称为标识符。</p>
<ul>
<li>标识符以字母、下划线、数字、<code>$</code>、<code>￥</code>组成</li>
<li>标识符可以是汉字，但不建议使用汉字作为标识符</li>
<li>不能以数字开头</li>
<li>不能是<code>java</code>语言关键字。</li>
</ul>
<p>合法标识符:<code>name123</code>,<code>name_123</code>,<code>_name123</code>,<code>$abc</code></p>
<p>非法标识符:<code>123name</code>，<code>&amp;name</code>,<code>#name</code></p>
<h2 id="常量"><a href="#常量" class="headerlink" title="常量"></a>常量</h2><p>运行过程中不变的量称为常量。如字符，字符串，数字，布尔值。</p>
<p><code>&#39;a&#39; , &quot;abc&quot;, 123 , ture , false</code></p>
<p>标识符可以作为常量，但要用<code>final修饰</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">final</span> <span class="keyword">int</span> arg = <span class="number">123</span>;</span><br></pre></td></tr></table></figure>
<h2 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h2><p>在运行过程中可以改变的量。<code>java</code>语言中采用标识符作为变量。</p>
<h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><p><code>java</code>中数据类型包括<strong><em>基本数据类型</em></strong>和<strong><em>引用数据类型</em></strong></p>
<h3 id="基本类型"><a href="#基本类型" class="headerlink" title="基本类型"></a>基本类型</h3><ul>
<li><p>整型 <code>byte, short, int, long</code></p>
</li>
<li><p>浮点型 <code>float ,double</code></p>
</li>
<li><p>字符型 <code>char</code></p>
</li>
<li><p>布尔型 <code>boolean</code></p>
<blockquote>
<p>注:自动类型转换:当运算时，若运算类型不一致，<code>jvm</code>将对数据类型进行自动转换，转换规则为:由低级向高级转换</p>
</blockquote>
</li>
</ul>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center"><code>\</code></th>
<th style="text-align:center">不参与转换</th>
<th style="text-align:center">低级</th>
<th style="text-align:center"><code>-&gt;</code></th>
<th style="text-align:center"><code>-&gt;</code></th>
<th style="text-align:center"><code>-&gt;</code></th>
<th style="text-align:center"><code>-&gt;</code></th>
<th style="text-align:center"><code>\</code></th>
<th style="text-align:center"><code>-&gt;</code></th>
<th style="text-align:center">高级</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">类型</td>
<td style="text-align:center"><code>boolean</code></td>
<td style="text-align:center"><code>byte</code></td>
<td style="text-align:center"><code>short</code></td>
<td style="text-align:center"><code>char</code></td>
<td style="text-align:center"><code>int</code></td>
<td style="text-align:center"><code>long</code></td>
<td style="text-align:center"><code>long long</code></td>
<td style="text-align:center"><code>float</code></td>
<td style="text-align:center"><code>double</code></td>
</tr>
<tr>
<td style="text-align:center">java</td>
<td style="text-align:center">1bit</td>
<td style="text-align:center">1B</td>
<td style="text-align:center">2B</td>
<td style="text-align:center">2B</td>
<td style="text-align:center">4B</td>
<td style="text-align:center">8B</td>
<td style="text-align:center"><code>\</code></td>
<td style="text-align:center">4B</td>
<td style="text-align:center">8B</td>
</tr>
<tr>
<td style="text-align:center">32位机c语言</td>
<td style="text-align:center">1bit</td>
<td style="text-align:center"><code>\</code></td>
<td style="text-align:center">2B</td>
<td style="text-align:center">1B</td>
<td style="text-align:center">4B</td>
<td style="text-align:center">4B</td>
<td style="text-align:center">8B</td>
<td style="text-align:center">4B</td>
<td style="text-align:center">8B</td>
</tr>
<tr>
<td style="text-align:center">64位机c语言</td>
<td style="text-align:center">1bit</td>
<td style="text-align:center"><code>\</code></td>
<td style="text-align:center">2B</td>
<td style="text-align:center">1B</td>
<td style="text-align:center">4B</td>
<td style="text-align:center">8B</td>
<td style="text-align:center">8B</td>
<td style="text-align:center">4B</td>
<td style="text-align:center">8B</td>
</tr>
</tbody>
</table>
</div>
<h3 id="引用类型"><a href="#引用类型" class="headerlink" title="引用类型"></a>引用类型</h3><p><code>java</code>中引用类型的变量非常类似于C/C++的指针，引用类型指向一个对象。</p>
<h4 id="引用类型-1"><a href="#引用类型-1" class="headerlink" title="引用类型"></a>引用类型</h4><p><code>java</code>中引用类型包括：</p>
<ul>
<li>类</li>
<li>接口</li>
<li>数组</li>
</ul>
<h4 id="包装类型"><a href="#包装类型" class="headerlink" title="包装类型"></a>包装类型</h4><p><code>java</code>语言将基本类型包装，得到对应的基本类型的包装类，称为包装类型。包装类型也是引用类型，相比与基本类型，包装类型有更多的操作，简化程序的编写过程。</p>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">基本数据类型</th>
<th style="text-align:center">包装类型</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>byte</code></td>
<td style="text-align:center"><code>Byte</code></td>
</tr>
<tr>
<td style="text-align:center"><code>shor</code></td>
<td style="text-align:center"><code>Short</code></td>
</tr>
<tr>
<td style="text-align:center"><code>int</code></td>
<td style="text-align:center"><code>Integer</code></td>
</tr>
<tr>
<td style="text-align:center"><code>long</code></td>
<td style="text-align:center"><code>Long</code></td>
</tr>
<tr>
<td style="text-align:center"><code>float</code></td>
<td style="text-align:center"><code>Float</code></td>
</tr>
<tr>
<td style="text-align:center"><code>double</code></td>
<td style="text-align:center"><code>Double</code></td>
</tr>
<tr>
<td style="text-align:center"><code>char</code></td>
<td style="text-align:center"><code>Character</code></td>
</tr>
<tr>
<td style="text-align:center"><code>boolean</code></td>
<td style="text-align:center"><code>Boolean</code></td>
</tr>
</tbody>
</table>
</div>
<p>自动装箱和自动拆箱是<code>Java SE5</code>后具有的特性，该特性使得包装类型和基本数据类型能具有相同的操作。</p>
<ul>
<li><p>自动装箱</p>
<p>将基本数据类型自动转换为包装数据类型。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Integer i = <span class="number">10</span>;<span class="comment">//自动装箱</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>自动拆箱</p>
<p>将包装数据类型自动转换为基本数据类型</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> ii = i;<span class="comment">//自动拆箱</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<blockquote>
<p>原本等号的左右两端的类型是不同的，一个是基本数据类型，一个是引用数据类型，不能用赋值符号直接赋值，但<code>Java SE5</code>后自动装箱在装箱的时候自动调用的是<code>Integer</code>的<code>valueOf(int)</code>方法。而在拆箱的时候自动调用的是<code>Integer</code>的<code>intValue()</code>方法。其他包装类同理。</p>
</blockquote>
<h2 id="关键字"><a href="#关键字" class="headerlink" title="关键字"></a>关键字</h2><p><code>java</code>关键字是<code>java</code>语言事先定义的具有特殊用途的标识符，关键字有53个，其中有2个保留字，保留字也是标识符，但不能被使用，是<code>java</code>语言保留下供未来特殊用途使用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span>    <span class="keyword">extends</span> <span class="keyword">implements</span> <span class="title">null</span>      <span class="title">strictfp</span>     <span class="title">true</span></span></span><br><span class="line"><span class="class"><span class="title">assert</span>   <span class="title">const</span>    <span class="title">false</span>   <span class="title">import</span>     <span class="title">package</span>   <span class="title">super</span>        <span class="title">try</span></span></span><br><span class="line"><span class="class"><span class="title">boolean</span>  <span class="title">continue</span> <span class="title">final</span>   <span class="title">instanceof</span> <span class="title">private</span>   <span class="title">switch</span>       <span class="title">void</span></span></span><br><span class="line"><span class="class"><span class="title">break</span>    <span class="title">default</span>  <span class="title">finally</span> <span class="title">int</span>        <span class="title">protected</span> <span class="title">synchronized</span> <span class="title">volatile</span></span></span><br><span class="line"><span class="class"><span class="title">byte</span>     <span class="title">do</span>       <span class="title">float</span>   <span class="title">interface</span>  <span class="title">public</span>    <span class="title">this</span>         <span class="title">while</span></span></span><br><span class="line"><span class="class"><span class="title">case</span>     <span class="title">double</span>   <span class="title">for</span>     <span class="title">long</span>       <span class="title">return</span>    <span class="title">throw</span></span></span><br><span class="line"><span class="class"><span class="title">catch</span>    <span class="title">else</span>     <span class="title">goto</span>    <span class="title">native</span>     <span class="title">short</span>     <span class="title">throws</span></span></span><br><span class="line"><span class="class"><span class="title">char</span>     <span class="title">enum</span>     <span class="title">if</span>      <span class="title">new</span>        <span class="title">static</span>    <span class="title">transient</span></span></span><br></pre></td></tr></table></figure>
<h2 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h2><ul>
<li><p>算术运算符</p>
<p><code>+,-,*,/,%,++,--</code></p>
</li>
</ul>
<ul>
<li><p>前缀自增、自减:先进行自增(+1)或者自减(-1)运算，再进行表达式运算。</p>
</li>
<li><p>后缀自增、自减:先进行表达式运算，再进行自增(+1)或者自减(-1)运算</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">int</span> b = <span class="number">5</span>;</span><br><span class="line"><span class="keyword">int</span> x = <span class="number">2</span>*++a;</span><br><span class="line"><span class="keyword">int</span> y = <span class="number">2</span>*b++;</span><br><span class="line">System.out.println(<span class="string">&quot;前缀a=&quot;</span>+a+<span class="string">&quot;,x=&quot;</span>+x);<span class="comment">//前缀a=6,x=12</span></span><br><span class="line">System.out.println(<span class="string">&quot;后缀b=&quot;</span>+b+<span class="string">&quot;,y=&quot;</span>+y);<span class="comment">//后缀b=6,y=10</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>关系运算符</p>
<p><code>==,!=,&gt;=,&lt;=,&gt;,&lt;</code></p>
</li>
</ul>
<ul>
<li><p>位运算符</p>
<p><code>&amp;,|,^,~,&lt;&lt;,&gt;&gt;,&gt;&gt;&gt;</code></p>
<p><code>java</code>中右移运算符有两个<code>&gt;&gt;</code>和<code>&gt;&gt;&gt;</code>,<code>&gt;&gt;</code>表示带符号右移,<code>&gt;&gt;&gt;</code>表示无符号右移</p>
<ul>
<li>对于正数，两个右移运算符无区别</li>
<li>对于负数，<code>&gt;&gt;&gt;</code>将带右移的数视作无符号整数，所有的位都向右移动指定位数，低位将被移除，高位被0补齐；<code>&gt;&gt;</code>将待右移的数是做带符号整数，只将表示数值的位进行右移，符号位不移动，低位移除，高位0补齐，</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a = -<span class="number">4</span>;</span><br><span class="line"><span class="keyword">int</span> b = <span class="number">4</span>;</span><br><span class="line">System.out.println(a &gt;&gt;&gt; <span class="number">1</span>);<span class="comment">//2147483646</span></span><br><span class="line">System.out.println(a &gt;&gt; <span class="number">1</span>);<span class="comment">//-2</span></span><br><span class="line">System.out.println(b &gt;&gt; <span class="number">1</span>);<span class="comment">//2</span></span><br><span class="line">System.out.println(b &gt;&gt;&gt; <span class="number">1</span>);<span class="comment">//2</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>逻辑运算符</p>
<p><code>&amp;&amp;,||,!</code></p>
<p>短路逻辑:当<code>&amp;&amp;</code>的左边为<code>false</code>时，<code>&amp;&amp;</code>的右边不再继续运算；当<code>||</code>的左边为<code>true</code>时，<code>||的右边不再运算</code></p>
</li>
<li><p>赋值运算符</p>
<p><code>=,+=,-=,*=,/=,%=,&lt;&lt;=,&gt;&gt;=,&amp;=,^=,|=</code></p>
</li>
<li><p>条件运算符</p>
<p><code>()=()?():()</code></p>
<p><code>value = (expression)?(if expression is true):(if expression is false)</code></p>
</li>
<li><p><code>instanceof</code>运算符</p>
<p>该运算符用于检测一个对象是否为某种类型或接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>&#123;</span><br><span class="line">    <span class="comment">//code ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">B</span> <span class="keyword">extends</span> <span class="title">A</span></span>&#123;</span><br><span class="line">	<span class="comment">//code ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        A a = <span class="keyword">new</span> A();</span><br><span class="line">        B b = <span class="keyword">new</span> B();</span><br><span class="line">        <span class="keyword">if</span> (a <span class="keyword">instanceof</span> B)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;a instance of B&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (b <span class="keyword">instanceof</span> A)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;b instance of A.&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">b instance of A.</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>运算符优先级:</p>
</li>
</ul>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">操作符</th>
<th style="text-align:center">类别</th>
<th style="text-align:center">关联性</th>
<th style="text-align:center"></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>() [] .</code></td>
<td style="text-align:center">后缀</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">高</td>
</tr>
<tr>
<td style="text-align:center"><code>expr++ expr--</code></td>
<td style="text-align:center">一元</td>
<td style="text-align:center">从左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>++expr --expr + - ～ ！</code></td>
<td style="text-align:center">一元</td>
<td style="text-align:center">从右到左</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>* / ％</code></td>
<td style="text-align:center">乘性</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>+ -</code></td>
<td style="text-align:center">加性</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>&gt;&gt; &gt;&gt;&gt;  &lt;&lt;</code></td>
<td style="text-align:center">移位</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>&gt; &gt;= &lt; &lt;=</code></td>
<td style="text-align:center">关系</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>== !=</code></td>
<td style="text-align:center">相等</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>＆</code></td>
<td style="text-align:center">按位与</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>^</code></td>
<td style="text-align:center">按位异或</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center">&#124;</td>
<td style="text-align:center">按位或</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>&amp;&amp;</code></td>
<td style="text-align:center">逻辑与</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center">&#124; &#124;</td>
<td style="text-align:center">逻辑或</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>?:</code></td>
<td style="text-align:center">条件</td>
<td style="text-align:center">从右到左</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>=,+=,-=,*=,/=,％=,&gt;&gt;=,&lt;&lt;=,＆=,^=,</code>&#124;<code>=</code></td>
<td style="text-align:center">赋值</td>
<td style="text-align:center">从右到左</td>
<td style="text-align:center">↓</td>
</tr>
<tr>
<td style="text-align:center"><code>,</code></td>
<td style="text-align:center">逗号</td>
<td style="text-align:center">左到右</td>
<td style="text-align:center">低</td>
</tr>
</tbody>
</table>
</div>
<h2 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h2><ul>
<li><p><code>if...else...</code></p>
</li>
<li><p><code>if...else if...else</code></p>
</li>
<li><p><code>switch</code></p>
<p><code>case</code>后面的值的类型要与<code>ch</code>的类型一致，否则会编译不通过</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">char</span> ch = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">switch</span> (ch) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;a&#x27;</span>:</span><br><span class="line">        System.out.println(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;b&#x27;</span>:</span><br><span class="line">        System.out.println(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        System.out.println(<span class="string">&quot;c&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="循环"><a href="#循环" class="headerlink" title="循环"></a>循环</h2><ul>
<li><p><code>while</code>循环</p>
</li>
<li><p><code>do ... while ...</code>循环</p>
</li>
<li><p><code>for</code>循环</p>
</li>
<li><p>增强<code>for</code>循环</p>
<p>增强<code>for</code>循环用于数组或者实现了<code>Iterable</code>接口的对象的遍历，是对for循环的简写。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] arr = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;;</span><br><span class="line">ArrayList&lt;Character&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">list.add(<span class="string">&#x27;a&#x27;</span>);</span><br><span class="line">list.add(<span class="string">&#x27;b&#x27;</span>);</span><br><span class="line">list.add(<span class="string">&#x27;c&#x27;</span>);</span><br><span class="line">list.add(<span class="string">&#x27;d&#x27;</span>);</span><br><span class="line">list.add(<span class="string">&#x27;e&#x27;</span>);</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i : arr)&#123;</span><br><span class="line">    System.out.print(i);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println();</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">char</span> ch: list)&#123;</span><br><span class="line">    System.out.print(ch);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">12345</span></span><br><span class="line"><span class="comment">abcde</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p><code>continue</code>关键字</p>
</li>
<li><p><code>break</code>关键字</p>
</li>
<li><p>带标签循环体</p>
<p>  可以为循环体定义一个标签，标签的名字是区别于关键字的标识符。可以使用<code>continue</code>和<code>break</code>直接跳到该标签处</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">label2:<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>;i &lt;= <span class="number">5</span>;i++)&#123;</span><br><span class="line">            label1:<span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">                i++;</span><br><span class="line">                <span class="keyword">if</span>(i == <span class="number">3</span>)&#123;</span><br><span class="line">                    System.out.println(<span class="string">&quot;i = 3&quot;</span>);</span><br><span class="line">                    i = <span class="number">9</span>;</span><br><span class="line">                    <span class="keyword">continue</span> label1;<span class="comment">//直接跳到label1定义的循环</span></span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (i == <span class="number">10</span>)&#123;</span><br><span class="line">                    System.out.println(<span class="string">&quot;i = 10&quot;</span>);</span><br><span class="line">                    <span class="keyword">break</span> label2;<span class="comment">//直接跳出label2定义的循环</span></span><br><span class="line">                &#125;</span><br><span class="line">                System.out.println(<span class="string">&quot;i = &quot;</span>+i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">i = 1</span></span><br><span class="line"><span class="comment">i = 2</span></span><br><span class="line"><span class="comment">i = 3</span></span><br><span class="line"><span class="comment">i = 10</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h1><h2 id="一维数组"><a href="#一维数组" class="headerlink" title="一维数组"></a>一维数组</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//声明和赋值同时进行</span></span><br><span class="line"><span class="keyword">int</span>[] arr1 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//动态声明后续赋值</span></span><br><span class="line"><span class="comment">// n 是变量</span></span><br><span class="line"><span class="keyword">int</span>[] arr2 = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line">arr2[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">arr2[<span class="number">1</span>] = <span class="number">2</span>;</span><br><span class="line">arr2[<span class="number">2</span>] = <span class="number">3</span>;</span><br><span class="line">...</span><br><span class="line">    </span><br><span class="line"><span class="comment">//创建并初始化</span></span><br><span class="line"><span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="多维数组"><a href="#多维数组" class="headerlink" title="多维数组"></a>多维数组</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//声明同时赋值，编译器将自动计算行和列</span></span><br><span class="line"><span class="keyword">int</span>[][] arr = &#123;</span><br><span class="line">    &#123; <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span> &#125;,</span><br><span class="line">    &#123; <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>&#125;,</span><br><span class="line">    &#123; <span class="number">9</span>, <span class="number">10</span> &#125;</span><br><span class="line">&#125;;</span><br><span class="line">System.out.println(<span class="string">&quot;arr 有 %d 行&quot;</span>.formatted(arr.length));</span><br><span class="line">System.out.println(<span class="string">&quot;arr[0] 有 %d 列&quot;</span>.formatted(arr[<span class="number">0</span>].length));</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span>[] i : arr)&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j:i)&#123;</span><br><span class="line">        System.out.println(j);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//动态声明，需要将指定行</span></span><br><span class="line"><span class="keyword">int</span>[][] arr1 = <span class="keyword">new</span> <span class="keyword">int</span>[][<span class="number">4</span>];<span class="comment">//不合法</span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span>[][] arr2 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>][];<span class="comment">//合法，但未指定列</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//后续对arr2的每列动态声明长度，该循环为arr2声明了一个3行4列的数组</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; <span class="number">3</span>;i++)&#123;</span><br><span class="line">	arr2[i] =<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//也可以将arr2声明为不定长二维数组</span></span><br><span class="line">arr2[<span class="number">0</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">2</span>];<span class="comment">//声明第一列长度为2</span></span><br><span class="line">arr2[<span class="number">1</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">1</span>];<span class="comment">//声明第二列长度为1</span></span><br><span class="line">arr2[<span class="number">2</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">10</span>];<span class="comment">//声明第三列长度10</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">int</span>[][] arr3 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">3</span>][<span class="number">4</span>];<span class="comment">//合法,3行4列数组</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="面向对象"><a href="#面向对象" class="headerlink" title="面向对象"></a>面向对象</h1><ul>
<li><p><code>OOA（Object-Oriented Analysis）</code>面向对象分析</p>
</li>
<li><p><code>OOD (Object-Oriented Design)</code>面向对象设计</p>
</li>
<li><p><code>OOP(Object-Oriented Programming)</code>面向对象编程</p>
</li>
</ul>
<h2 id="访问控制"><a href="#访问控制" class="headerlink" title="访问控制"></a>访问控制</h2><ul>
<li>成员访问控制</li>
</ul>
<p>被访问控制作用的成员变量和成员函数只能在指定的使用范围内使用。<code>java</code>的成员访问控制有如下四种:</p>
<pre><code>- `private`
    - `default`
    - `protected`
    - `public`
</code></pre><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">使用范围</th>
<th style="text-align:center">private</th>
<th style="text-align:center">default</th>
<th style="text-align:center">protected</th>
<th style="text-align:center">public</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">同一类中</td>
<td style="text-align:center">√</td>
<td style="text-align:center">√</td>
<td style="text-align:center">√</td>
<td style="text-align:center">√</td>
</tr>
<tr>
<td style="text-align:center">同一包中子类和非子类</td>
<td style="text-align:center">×</td>
<td style="text-align:center">√</td>
<td style="text-align:center">√</td>
<td style="text-align:center">√</td>
</tr>
<tr>
<td style="text-align:center">不同包中的子类</td>
<td style="text-align:center">×</td>
<td style="text-align:center">×</td>
<td style="text-align:center">√</td>
<td style="text-align:center">√</td>
</tr>
<tr>
<td style="text-align:center">不同包中的非子类</td>
<td style="text-align:center">×</td>
<td style="text-align:center">×</td>
<td style="text-align:center">×</td>
<td style="text-align:center">√</td>
</tr>
</tbody>
</table>
</div>
<ul>
<li>类访问控制<br>  类访问控制只有两种:<ul>
<li><code>default</code></li>
<li><code>public</code></li>
</ul>
</li>
</ul>
<p>​    作用范围和成员访问控制一样</p>
<blockquote>
<p>default 即默认的访问控制，不能写default关键字来声明</p>
</blockquote>
<h2 id="类"><a href="#类" class="headerlink" title="类"></a>类</h2><ul>
<li><p>属性</p>
<p>定义在类中的变量被称作属性。</p>
</li>
<li><p>方法</p>
<p>定义在类中的函数被称为方法。方法不能修改基本数据类型的参数，可以修改对象引用的参数。</p>
<blockquote>
<p>方法也不能修改String类的参数，java的形参传递的是参数的拷贝，String类被传入方法中被使用时实际上是在常量池拷贝了一份一样的字符串。而其他类作为参数时，拷贝的是该类的对象引用，即地址值，地址指向的区域数据被修改，那么实际上该对象的属性就被修改了。</p>
</blockquote>
<ul>
<li>方法可变参数</li>
</ul>
</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        variable(<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>);</span><br><span class="line">        variable(<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">variable</span><span class="params">(<span class="keyword">int</span>... args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i : args) &#123;</span><br><span class="line">            System.out.print(i);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">0123456</span></span><br><span class="line"><span class="comment">0123</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<ul>
<li><p>方法重载与方法重写</p>
<p>重载是一个方法对应于不同的参数类型或不同的参数个数，根据调用时使用的参数不同，调用的方法体不同。</p>
<p>重写是某个方法继承于父类的方法，需要覆盖父类的该方法，该方法与父类有相同的方法名，相同的参数类型，相同的参数个数，相同的返回值，不同的方法体。</p>
<p>重载：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">test</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b,<span class="keyword">int</span> c)</span></span>&#123;</span><br><span class="line">	...</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">test</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">	...</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">test</span><span class="params">(String s,<span class="keyword">int</span> a)</span></span>&#123;</span><br><span class="line">	...</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span>  <span class="title">test</span><span class="params">(String s,String a)</span></span>&#123;</span><br><span class="line">	...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>重写:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    String s;</span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>类</p>
<p>类是对象的蓝图，是现实中一类具有共同属性和行为的事物的抽象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> age = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;动物吃&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此处<code>Animal</code>是一个对象，该对象拥有一个<code>age</code>属性和一个<code>eat()</code>方法。</p>
</li>
<li><p>对象</p>
<p>是类的具体实例，对象拥有类的属性和方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        Animal animal = <span class="keyword">new</span> Animal();</span><br><span class="line">        System.out.println(animal.age);</span><br><span class="line">        animal.eat();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此处<code>animal</code>是一个对象，使用<code>new</code>关键字创建了<code>Animal</code>类的一个实例，该实例即为一个对象。该对象具有<code>Animal</code>的属性和方法。</p>
</li>
<li><p>内部类</p>
<p>定义在类的内部的类，此处Inner为内部类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Outer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">in</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;类方法in()&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Inner</span></span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">in</span><span class="params">()</span></span>&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;内部类方法in()&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Outer outer = <span class="keyword">new</span> Outer();</span><br><span class="line">        outer.in();</span><br><span class="line"></span><br><span class="line">        Outer.Inner inner = outer.<span class="function">new <span class="title">Inner</span><span class="params">()</span></span>;</span><br><span class="line">        inner.in();</span><br><span class="line">        <span class="comment">//Outer.Inner inner = new Outer().new Inner();</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">类方法in()</span></span><br><span class="line"><span class="comment">内部类方法in()</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<ul>
<li><p>局部内部类</p>
<p>定义在方法里的类，该类只能在方法中使用。</p>
<p>特点：</p>
<ul>
<li>内部类可以直接访问”外部类”的成员</li>
<li>“外部类”要访问内部类的成员要创建对象</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Outer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">in</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="class"><span class="keyword">class</span> <span class="title">Inner</span></span>&#123;</span><br><span class="line">            <span class="function"><span class="keyword">void</span> <span class="title">in</span><span class="params">()</span></span>&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;局部内部类方法in()&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        Inner inner = <span class="keyword">new</span> Inner();</span><br><span class="line">        inner.in();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Outer outer = <span class="keyword">new</span> Outer();</span><br><span class="line">        outer.in();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">局部内部类方法in()</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>匿名内部类</p>
<p>匿名内部类是没有名字的类，该类继承自某个类或者实现了某个接口。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Outer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//实现了Outer接口并调用了接口的show()方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">new</span> Outer() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;Outer 接口&quot;</span>);   </span><br><span class="line">            &#125;</span><br><span class="line">        &#125;.show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">Outer 接口</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//若想多次调用该匿名内部类的方法，可以为该类创建对象，</span></span><br><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">          Outer outer = <span class="keyword">new</span> Outer() &#123;</span><br><span class="line">              <span class="meta">@Override</span></span><br><span class="line">              <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                  System.out.println(<span class="string">&quot;Outer 接口&quot;</span>);</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;;</span><br><span class="line">          outer.show();</span><br><span class="line">          outer.show();</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">/*</span></span><br><span class="line"><span class="comment">  输出：</span></span><br><span class="line"><span class="comment">  Outer 接口</span></span><br><span class="line"><span class="comment">  Outer 接口</span></span><br><span class="line"><span class="comment">  </span></span><br><span class="line"><span class="comment">  */</span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当接口只有一个方法时，可以使用箭头函数</span></span><br><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">          ((Outer) () -&gt; System.out.println(<span class="string">&quot;Outer 接口&quot;</span>)).show();</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">/*</span></span><br><span class="line"><span class="comment">  输出：</span></span><br><span class="line"><span class="comment">  Outer 接口</span></span><br><span class="line"><span class="comment">  */</span></span><br></pre></td></tr></table></figure>
<p>箭头函数参考：<a href="#Lambda表达式">lambda</a></p>
<p>匿名内部类的应用：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Sleep</span> </span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span></span>;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">void</span> <span class="title">sleap</span><span class="params">(Sleep s)</span></span>&#123;</span><br><span class="line">          s.sleep();</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">          Animal animal = <span class="keyword">new</span> Animal();</span><br><span class="line">          animal.sleap(<span class="keyword">new</span> Sleep() &#123;</span><br><span class="line">              <span class="meta">@Override</span></span><br><span class="line">              <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                  System.out.println(<span class="string">&quot;马站着睡！&quot;</span>);</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;);</span><br><span class="line">  </span><br><span class="line">          animal.sleap(<span class="keyword">new</span> Sleep() &#123;</span><br><span class="line">              <span class="meta">@Override</span></span><br><span class="line">              <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                  System.out.printf(<span class="string">&quot;人躺着睡！&quot;</span>);</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;);</span><br><span class="line">  <span class="comment">//        animal.sleap(() -&gt; System.out.println(&quot;马站着睡！&quot;));</span></span><br><span class="line">  <span class="comment">//        animal.sleap(() -&gt; System.out.printf(&quot;人躺着睡！&quot;));</span></span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">/*</span></span><br><span class="line"><span class="comment">  输出：</span></span><br><span class="line"><span class="comment">  马站着睡！</span></span><br><span class="line"><span class="comment">  人躺着睡！</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  </span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>抽象类</p>
<p>抽象类是对类的抽象，不能像一般类一样刻画某一个对象的细节，只能声明某个类有什么属性和方法。在<code>java</code>中一个没有方法体的方法应该被定义为抽象方法，一个有抽象方法的类应该被定义为抽象类。抽象类因为没有具体的细节来描述一个对象，所以不能被实例化，因此不能用<code>new</code>关键字来实例化一个对象，它只能被他的子类继承并实现它的方法。</p>
<p>抽象类和抽象方法都用关键字<code>abstract</code>修饰</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> age;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;动物都要吃东西!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">sleap</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h2><p>接口是对事物行为的抽象，其中的方法规定了实现它接口的类必须做什么而不关心怎么做。</p>
<p>定义一个接口使用<code>interface</code>关键字。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Sleep</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>接口中的方法默认为<code>public abstract</code>方法</p>
</li>
<li><p>接口中的属性都是常量，默认为<code>public static final</code></p>
</li>
<li><p>使用<code>default</code>关键字修饰的方法可以有实现</p>
</li>
<li><p>接口中可以有私有方法，但私有方法不能被继承所以被定义的私有方法必须被实现</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Sleep</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">default</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//code ...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>接口中使用<code>static</code>关键字修饰的方法只能通过<code>接口名称.方法名称</code>调用。</p>
</li>
<li><p>接口不能包含成员变量，除了 static 和 final 变量。</p>
</li>
</ul>
<blockquote>
<p>抽象类和接口的区别:</p>
<ul>
<li>接口是对行为的抽象，抽象类是对事物的抽象</li>
<li>接口没有构造方法，而抽象类可以有构造方法</li>
<li>接口中不能含有静态代码块，抽象类中可以有静态代码块</li>
<li>一个类只能继承于一个抽象类，但可以实现多个接口</li>
</ul>
</blockquote>
<h2 id="枚举"><a href="#枚举" class="headerlink" title="枚举"></a>枚举</h2><p>枚举类表示常量</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">enum</span> <span class="title">Week</span> </span>&#123;</span><br><span class="line">    Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Week a = Week.Friday;</span><br><span class="line">        System.out.printf(String.valueOf(a));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出:</span></span><br><span class="line"><span class="comment">Friday</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>所有的类都有个clone()方法，该方法被protected修饰，需要重写后使用。该方法属于浅拷贝。</p>
</blockquote>
<h2 id="常用类"><a href="#常用类" class="headerlink" title="常用类"></a>常用类</h2><ul>
<li><code>Calendar</code></li>
<li><code>Date</code></li>
<li><code>String</code>和<code>StringBuilder</code></li>
<li><code>Random</code></li>
<li><code>BigInteger</code>处理任意精度的整数</li>
<li><p><code>BigDecimal</code>处理任意精度的浮点数</p>
</li>
<li><p>类之间的关系</p>
<ul>
<li>依赖(“uses-a”)一个类的方法中操作了某个对象</li>
<li>聚合(“has-a”)类中含有某个类的对象</li>
<li>继承(“is-a”)继承于某个类</li>
</ul>
</li>
</ul>
<h2 id="static"><a href="#static" class="headerlink" title="static"></a>static</h2><ul>
<li>使用<code>static</code>修饰方法表示该方法是静态方法</li>
<li>使用<code>static</code>修饰修饰属性表示该属性是静态属性</li>
<li>使用<code>static</code>修饰的代码块表示该代码块是静态代码块</li>
</ul>
<blockquote>
<p>使用static关键字修饰的方法或属性本质上不属于该类，但可以理解为它属于该类但不属于类创建的对象，但可以通过该类或得该属性和方法。</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> String className = <span class="string">&quot;main&quot;</span>;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;这是Main类中的静态方法!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Main 类被加载!&quot;</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(Main.className);</span><br><span class="line">        Main.method();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="四大特性"><a href="#四大特性" class="headerlink" title="四大特性"></a>四大特性</h2><ul>
<li><p>多态</p>
<p>同一类事物，不同对象所表现的行为不一样。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Child</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;儿子吃饭!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PoorChild</span> <span class="keyword">extends</span> <span class="title">Child</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;儿子吃空气!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RichChild</span> <span class="keyword">extends</span> <span class="title">Child</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;儿子吃山珍海味!&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Child child = <span class="keyword">new</span> Child();</span><br><span class="line">        child.eat();</span><br><span class="line">        Child poorChild = <span class="keyword">new</span> PoorChild();</span><br><span class="line">        poorChild.eat();</span><br><span class="line">        Child richChild = <span class="keyword">new</span> RichChild();</span><br><span class="line">        poorChild.eat();</span><br><span class="line">       </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出:</span></span><br><span class="line"><span class="comment">儿子吃饭!</span></span><br><span class="line"><span class="comment">儿子吃空气!</span></span><br><span class="line"><span class="comment">儿子吃山珍海味!</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<blockquote>
<p>多态口诀:编译看左边，执行看右边</p>
</blockquote>
</li>
<li><p>继承</p>
</li>
<li><p>封装</p>
<p>将属性封装在类中，使用访问控制进行数据保护，将方法封装在类中，隐藏具体的实现细节，减少耦合，方便使用易用性。</p>
</li>
<li><p>抽象</p>
</li>
</ul>
<h1 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h1><h2 id="泛型的好处"><a href="#泛型的好处" class="headerlink" title="泛型的好处"></a>泛型的好处</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Collection collection = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        collection.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        collection.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        collection.add(<span class="number">1000</span>);</span><br><span class="line">        Iterator iterator = collection.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext())&#123;</span><br><span class="line">            System.out.println(iterator.next());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">hello</span></span><br><span class="line"><span class="comment">world</span></span><br><span class="line"><span class="comment">1000</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<ul>
<li>将运行时期的异常提前到了编译期</li>
</ul>
<p>当未指定<code>Collection</code>中存储的类型时，默认任为<code>&lt;Object&gt;</code>,指定类型后可以编译器将检查存储数据的类型，所以当<code>collection.add(100)</code>时报错</p>
<p><img src="http://124.221.123.212:8080/images/image-20220313160501925.png" alt="image-20220313160501925"></p>
<ul>
<li>避免强制转换</li>
</ul>
<p><img src="http://124.221.123.212:8080/images/image-20220313160652742.png" alt="image-20220313160652742"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Collection&lt;String&gt; collection = <span class="keyword">new</span> ArrayList&lt;String&gt;();</span><br><span class="line">        collection.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        collection.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        Iterator&lt;String&gt; iterator = collection.iterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext())&#123;</span><br><span class="line">            String s = iterator.next();</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">hello</span></span><br><span class="line"><span class="comment">world</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h2 id="泛型类"><a href="#泛型类" class="headerlink" title="泛型类"></a>泛型类</h2> <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Generic</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    T t;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">setT</span><span class="params">(T t)</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.t = t;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function">T <span class="title">getT</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Generic&lt;String&gt; s = <span class="keyword">new</span> Generic&lt;&gt;();</span><br><span class="line">        s.setT(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        System.out.println(s.getT());</span><br><span class="line"></span><br><span class="line">        Generic&lt;Integer&gt; i = <span class="keyword">new</span> Generic&lt;&gt;();</span><br><span class="line">        i.setT(<span class="number">1000</span>);</span><br><span class="line">        System.out.println(i.getT());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">hello</span></span><br><span class="line"><span class="comment">1000</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>定义多参数列表泛型类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Generic</span>&lt;<span class="title">T</span>,<span class="title">E</span>,<span class="title">V</span>&gt; </span>&#123;</span><br><span class="line">    V v;</span><br><span class="line">    &lt;T&gt; <span class="function">T <span class="title">get</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> t;</span><br><span class="line">    &#125;</span><br><span class="line">    &lt;E&gt; <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">(E e)</span></span>&#123;</span><br><span class="line">        System.out.println(e);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">setV</span><span class="params">(V v)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.v = v;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="泛型方法"><a href="#泛型方法" class="headerlink" title="泛型方法"></a>泛型方法</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Generic</span></span>&#123;</span><br><span class="line">    &lt;T&gt; <span class="function">T <span class="title">show</span><span class="params">(T t)</span></span>&#123;</span><br><span class="line">        System.out.println(t);</span><br><span class="line">        <span class="keyword">return</span> t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Generic s = <span class="keyword">new</span> Generic();</span><br><span class="line">        String name = s.show(<span class="string">&quot;孙悟空&quot;</span>);</span><br><span class="line">        <span class="keyword">int</span> age = s.show(<span class="number">18</span>);</span><br><span class="line">        System.out.println(name+age);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">孙悟空</span></span><br><span class="line"><span class="comment">18</span></span><br><span class="line"><span class="comment">孙悟空18</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h2 id="泛型接口"><a href="#泛型接口" class="headerlink" title="泛型接口"></a>泛型接口</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//泛型接口的定义和泛型类定义一样</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Generic</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">    &lt;T&gt; <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">(T t)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//泛型接口的是仙女要加泛型参数T</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericImp</span>&lt;<span class="title">T</span>&gt; <span class="keyword">implements</span> <span class="title">Generic</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">        System.out.println(t);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        GenericImp genericImp = <span class="keyword">new</span> GenericImp();</span><br><span class="line">        genericImp.show(<span class="string">&quot;孙悟空&quot;</span>);</span><br><span class="line">        genericImp.show(<span class="number">18</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">孙悟空</span></span><br><span class="line"><span class="comment">18</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h2 id="泛型通配符"><a href="#泛型通配符" class="headerlink" title="泛型通配符"></a>泛型通配符</h2><p><code>&lt;?&gt;</code>可以代表任意的类型，代表<code>Object</code>及其子类</p>
<p><code>&lt;? extends Number&gt;</code>，泛型通配符上限，代表<code>Number</code>及其子类</p>
<p><code>&lt;? super Number&gt;</code>,泛型通配符下限，代表<code>Number</code>及其父类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">List&lt;?&gt; list = <span class="keyword">new</span> ArrayList&lt;Object&gt;();<span class="comment">//合法</span></span><br><span class="line">List&lt;? extends Number&gt; list1 = <span class="keyword">new</span> ArrayList&lt;Number&gt;();<span class="comment">//合法</span></span><br><span class="line">List&lt;? extends Number&gt; list2 = <span class="keyword">new</span> ArrayList&lt;Object&gt;();<span class="comment">//非法</span></span><br><span class="line">List&lt;? <span class="keyword">super</span> Number&gt; list3 = <span class="keyword">new</span> ArrayList&lt;Number&gt;();<span class="comment">//合法</span></span><br><span class="line">List&lt;? <span class="keyword">super</span> Number&gt; list4 = <span class="keyword">new</span> ArrayList&lt;Object&gt;();<span class="comment">//合法</span></span><br><span class="line">List&lt;? <span class="keyword">super</span> Number&gt; list5 = <span class="keyword">new</span> ArrayList&lt;Integer&gt;();<span class="comment">//非法</span></span><br></pre></td></tr></table></figure>
<h1 id="Stream-API"><a href="#Stream-API" class="headerlink" title="Stream API"></a>Stream API</h1><h2 id="编程思想"><a href="#编程思想" class="headerlink" title="编程思想"></a>编程思想</h2><ul>
<li><p>函数式编程思想</p>
<p>强调做什么而不强调怎么做。</p>
</li>
<li><p>面向对象编程思想</p>
<p>必须通过对象来做某件事情。</p>
</li>
</ul>
<h2 id="Lambda表达式"><a href="#Lambda表达式" class="headerlink" title="Lambda表达式"></a>Lambda表达式</h2><h3 id="Lambda表达式格式"><a href="#Lambda表达式格式" class="headerlink" title="Lambda表达式格式"></a>Lambda表达式格式</h3><p>Lambda表达式相当于一个匿名的函数，没有指出函数名和返回结果类型，但<code>jvm</code>可以对其进行推导。Lambda表达式程序编写更加简洁。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">([Type1 param1,Type2 param2 ...]) -&gt; &#123;</span><br><span class="line">    <span class="comment">//code...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>案例:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Sleep</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">sleep</span><span class="params">(Sleep s)</span></span>&#123;</span><br><span class="line">        s.sleep();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Horse</span> <span class="keyword">implements</span> <span class="title">Sleep</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;马站着睡！&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Animal animal = <span class="keyword">new</span> Animal();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//类实现类接口</span></span><br><span class="line">        Sleep horse = <span class="keyword">new</span> Horse();</span><br><span class="line">        animal.sleep(horse);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//利用内部类实现接口</span></span><br><span class="line">        animal.sleep(<span class="keyword">new</span> Sleep() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;老虎趴着睡！&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//利用lambda表达式实现接口,省略模式</span></span><br><span class="line">        animal.sleep(()-&gt; System.out.println(<span class="string">&quot;人躺着睡！&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment">马站着睡！</span></span><br><span class="line"><span class="comment">老虎趴着睡！</span></span><br><span class="line"><span class="comment">人躺着睡！</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>抽象方法无参：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Sleep</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        sleep(()-&gt; System.out.println(<span class="string">&quot;人躺着睡觉!&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">(Sleep s)</span></span>&#123;</span><br><span class="line">        s.sleep();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>抽象方法带参：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Sleep</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">sleep</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">(Sleep s)</span></span>&#123;</span><br><span class="line">        s.sleep(<span class="number">8</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        sleep((<span class="keyword">int</span> x)-&gt; System.out.println(<span class="string">&quot;人躺着睡觉!睡 %d 个小时&quot;</span>.formatted(x)));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>抽象方法带参带返回值:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Sleep</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">sleep</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> s = sleep((<span class="keyword">int</span> x)-&gt;&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;人躺着睡觉!睡 %d 个小时&quot;</span>.formatted(x));</span><br><span class="line">            <span class="keyword">return</span> x;</span><br><span class="line">        &#125;);</span><br><span class="line">        System.out.println(s);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">sleep</span><span class="params">(Sleep s)</span></span>&#123;</span><br><span class="line">        s.sleep(<span class="number">8</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">8</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Num</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> s = num((<span class="keyword">int</span> x,<span class="keyword">int</span> y)-&gt;&#123;</span><br><span class="line">            <span class="keyword">return</span> x + y;</span><br><span class="line">        &#125;);</span><br><span class="line">        System.out.println(s);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">num</span><span class="params">(Num n)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> n.add(<span class="number">10</span>,<span class="number">11</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Lambda表达式省略模式"><a href="#Lambda表达式省略模式" class="headerlink" title="Lambda表达式省略模式"></a>Lambda表达式省略模式</h3><ul>
<li><p>没有返回值时，省略参数类型,但要么全部省略要不省略</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Num</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    num((x, y) -&gt;  &#123;</span><br><span class="line">        System.out.println(x + y);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">num</span><span class="params">(Num n)</span> </span>&#123;</span><br><span class="line">    n.add(<span class="number">10</span>, <span class="number">11</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>没有返回值，且当参数只有一个时可以省去参数的括号</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Num</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        num(x -&gt;  &#123;System.out.println( x);&#125;);</span><br><span class="line">    </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">num</span><span class="params">(Num n)</span> </span>&#123;</span><br><span class="line">    n.add(<span class="number">10</span>);</span><br><span class="line">    </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    x-&gt;System.out.println(x)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">- 没有返回值，且当代码块只有一句话时可以省去大括号</span><br><span class="line"></span><br><span class="line">  ```java</span><br><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Num</span> </span>&#123;</span><br><span class="line">      <span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">      num(x -&gt; System.out.println(x));</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">num</span><span class="params">(Num n)</span> </span>&#123;</span><br><span class="line">      n.add(<span class="number">10</span>);</span><br><span class="line">  </span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>有返回值，且当代码块只有一句话时，若省掉大阔话，那么return 也该省略</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Num</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> s = num((x,y)-&gt; x+y);</span><br><span class="line">    System.out.println(s);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">num</span><span class="params">(Num n)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> n.add(<span class="number">10</span>,<span class="number">11</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<blockquote>
<p>使用Lambda表达式的前提：</p>
<ul>
<li>必须要有接口，且接口中只有一个抽象方法，若有多个抽象方法，可以使用匿名内部类</li>
<li>必须要有上下文环境，编译器才能通过上下文环境推导出Lambda表达式对应的接口类型<ul>
<li>可以通过局部变量赋值推导</li>
<li>可以通过方法的参数类型推导</li>
</ul>
</li>
<li>Lambda表达式可以被推导的类型只能是接口，参数或者局部变量为匿名内部类和抽象类时不能使用Lambda表达式</li>
</ul>
<p>Lambda表达式和匿名内部类的区别:</p>
<ul>
<li>所需类型不同<br>匿名内部类：可以是接口，也可以是抽象类，还可以是具体类<br>Lambda表达式：只能是接口</li>
<li>使用限制不同<br>如果接口中有且仅有一个抽象方法，可以使用Lambda表达式，也可以使用匿名内部类<br>如果接口中多于一个抽象方法，只能使用匿名内部类，而不能使用Lambda表达式</li>
<li>实现原理不同<br>匿名内部类：编译之后，产生一个单独的.class字节码文件<br>Lambda表达式：编译之后，没有一个单独的.class字节码文件,对应的字节码会在运行的时候动态生成</li>
</ul>
</blockquote>
<h2 id="方法引用"><a href="#方法引用" class="headerlink" title="方法引用"></a>方法引用</h2><ul>
<li><p>引用类方法（引用类的静态方法）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Convert</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">convert</span><span class="params">(String s)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        userConvert(s -&gt; Integer.parseInt(s));<span class="comment">//Lambda表达式</span></span><br><span class="line">        userConvert(Integer::parseInt);<span class="comment">//方法引用，类的静态方法</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">userConvert</span><span class="params">(Convert c)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> result = c.convert(<span class="string">&quot;666&quot;</span>);</span><br><span class="line">        System.out.println(result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>引用对象的实例方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Upper</span></span>&#123;</span><br><span class="line">    <span class="function">String <span class="title">upper</span><span class="params">(String s)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CastUpper</span> <span class="keyword">implements</span> <span class="title">Upper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">upper</span><span class="params">(String s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span>  s.toUpperCase();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        CastUpper castUpper = <span class="keyword">new</span>  CastUpper();</span><br><span class="line">        cast(castUpper::upper);<span class="comment">//对象的实例方法</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">cast</span><span class="params">(Upper c)</span></span>&#123;</span><br><span class="line">        String s = c.upper(<span class="string">&quot;Hello world&quot;</span>);</span><br><span class="line">        System.out.println(s);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>引用类的实例方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">SubString</span></span>&#123;</span><br><span class="line">    <span class="function">String <span class="title">substr</span><span class="params">(String s,<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        userSub((s,x,y) -&gt; s.substring(x,y));<span class="comment">//Lambda表达式</span></span><br><span class="line">        userSub(String::substring);<span class="comment">//类的实例方法，当参数有多个时，第一个参数默认为类的实例</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">userSub</span><span class="params">(SubString c)</span></span>&#123;</span><br><span class="line">        String result = c.substr(<span class="string">&quot;012345&quot;</span>,<span class="number">2</span>,<span class="number">5</span>);</span><br><span class="line">        System.out.println(result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>引用构造器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> age;</span><br><span class="line">    String name;</span><br><span class="line">    Student(<span class="keyword">int</span> age,String name)&#123;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function">String <span class="title">getName</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&quot;, name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">StudentBuilder</span> </span>&#123;</span><br><span class="line">    <span class="function">Student <span class="title">build</span><span class="params">(<span class="keyword">int</span> age,String name)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        useStudentBuilder((age,name) -&gt; <span class="keyword">new</span> Student(age,name));<span class="comment">//Lambda表达式</span></span><br><span class="line">        useStudentBuilder(Student::<span class="keyword">new</span>);<span class="comment">//引用类构造器</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">useStudentBuilder</span><span class="params">(StudentBuilder sb)</span> </span>&#123;</span><br><span class="line">        Student student = sb.build(<span class="number">18</span>,<span class="string">&quot;孙悟空&quot;</span>);</span><br><span class="line">        System.out.println(student);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">Student&#123;age=18, name=&#x27;孙悟空&#x27;&#125;</span></span><br><span class="line"><span class="comment">Student&#123;age=18, name=&#x27;孙悟空&#x27;&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="函数式接口"><a href="#函数式接口" class="headerlink" title="函数式接口"></a>函数式接口</h2><p>函数式接口指的是有且只有一个方法的接口</p>
<h3 id="定义函数式接口"><a href="#定义函数式接口" class="headerlink" title="定义函数式接口"></a>定义函数式接口</h3><p>加上注解<code>@FunctionalInterface</code>表示函数式接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>若不加<code>@FunctionalInterface</code>，编译器不会检测接口是否是函数式接口，若加了，编译器会检测该接口是否只有一个方法。若接口只有一个方法时，加注解或者不加都表示函数式接口</p>
</blockquote>
<p><code>import java.util.function</code>包下自带的常用函数式接口</p>
<ul>
<li><code>Supplier</code></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        String s = getT(()-&gt;<span class="string">&quot;孙悟空&quot;</span>);</span><br><span class="line">        Integer age = getT(() -&gt; <span class="number">18</span>);</span><br><span class="line">        System.out.println(s + age);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">T <span class="title">getT</span><span class="params">(Supplier&lt;T&gt; supplier)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> supplier.get();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li><code>Consumer</code></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Consumer</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">accept</span><span class="params">(T t)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">default</span> Consumer&lt;T&gt; <span class="title">andThen</span><span class="params">(Consumer&lt;? <span class="keyword">super</span> T&gt; after)</span> </span>&#123;</span><br><span class="line">        Objects.requireNonNull(after);</span><br><span class="line">        <span class="keyword">return</span> (T t) -&gt; &#123; accept(t); after.accept(t); &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        operatorString(<span class="string">&quot;孙悟空&quot;</span>, s -&gt; System.out.println(s), s -&gt;  System.out.println(<span class="keyword">new</span> StringBuilder(s).reverse()));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">operatorString</span><span class="params">(String name, Consumer&lt;String&gt; c1, Consumer&lt;String&gt; c2)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">        c1.andThen(c2).accept(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">孙悟空</span></span><br><span class="line"><span class="comment">空悟孙</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">c1.andThen(c2)相当于又创建了一个Consumer的实例对象consumer,该Consumer大致如下:</span></span><br><span class="line"><span class="comment">public interface Consumer&#123;</span></span><br><span class="line"><span class="comment">    void accept(String t)&#123;</span></span><br><span class="line"><span class="comment">        accept(t);//该accept调用的是c1.accept(t);</span></span><br><span class="line"><span class="comment">        affter.accept(t);//该accept调用的是c2.accept(t);</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">当consumer.accept(name)时，相当于执行了c1.accept(name)；c2.accept(name)；</span></span><br><span class="line"><span class="comment"></span></span><br></pre></td></tr></table></figure>
<ul>
<li><p><code>Predicate</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pre.negate().test(t);</span><br><span class="line">pre.and(other).test(t);</span><br><span class="line">pre.or(other).test(t);</span><br><span class="line">pre.not(other).test(t);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="函数式接口Function"><a href="#函数式接口Function" class="headerlink" title="函数式接口Function"></a>函数式接口<code>Function</code></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Function&lt;T,R&gt; T为参数，R为该函数式接口的返回值</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Student student = function(<span class="string">&quot;孙悟空&quot;</span>,Student::<span class="keyword">new</span>);</span><br><span class="line">        System.out.println(student);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Student <span class="title">function</span><span class="params">(String name, Function&lt;String, Student&gt; fun)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> fun.apply(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">Student&#123;name=&#x27;孙悟空&#x27;&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h2 id="Stream-API-1"><a href="#Stream-API-1" class="headerlink" title="Stream API"></a>Stream API</h2><h3 id="Stream-API提供的方法"><a href="#Stream-API提供的方法" class="headerlink" title="Stream API提供的方法"></a>Stream API提供的方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">ArrayList&lt;String&gt; list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">list.add(<span class="string">&quot;张三&quot;</span>);</span><br><span class="line">list.add(<span class="string">&quot;李四&quot;</span>);</span><br><span class="line">list.add(<span class="string">&quot;王五&quot;</span>);</span><br><span class="line">list.add(<span class="string">&quot;钱六&quot;</span>);</span><br><span class="line"></span><br><span class="line">ArrayList&lt;String&gt; list1 = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">list1.add(<span class="string">&quot;刘一&quot;</span>);</span><br><span class="line">list1.add(<span class="string">&quot;陈二&quot;</span>);</span><br><span class="line">list1.add(<span class="string">&quot;孙七&quot;</span>);</span><br><span class="line">list1.add(<span class="string">&quot;周八&quot;</span>);</span><br><span class="line">list1.add(<span class="string">&quot;张三&quot;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li><p><code>foreach(Comsumer comsumer)遍历流中的元素</code></p>
</li>
<li><p><code>filter(Predicate predicate)过滤元素</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list.stream().filter(s -&gt;s.startsWith(<span class="string">&quot;张&quot;</span>)).forEach(System.out::println);</span><br><span class="line"><span class="comment">//        张三</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><code>limit(long maxSize)截取[0,maxSize)的元素</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list.stream().limit(<span class="number">2</span>).forEach(System.out::println);</span><br><span class="line"><span class="comment">//        张三</span></span><br><span class="line"><span class="comment">//        李四</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p><code>skip(long n)跳过索引为[0,n)以前的元素</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list.stream().skip(<span class="number">2</span>).forEach(System.out::println);</span><br><span class="line"><span class="comment">//        王五</span></span><br><span class="line"><span class="comment">//		  钱六</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p><code>concat(Stream s1,Stream s2)将两个流合并</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Stream.concat(list1.stream(),list.stream()).forEach(System.out::println);</span><br><span class="line"><span class="comment">//        刘一</span></span><br><span class="line"><span class="comment">//        陈二</span></span><br><span class="line"><span class="comment">//        孙七</span></span><br><span class="line"><span class="comment">//        周八</span></span><br><span class="line"><span class="comment">//        张三</span></span><br><span class="line"><span class="comment">//        张三</span></span><br><span class="line"><span class="comment">//        李四</span></span><br><span class="line"><span class="comment">//        王五</span></span><br><span class="line"><span class="comment">//        钱六</span></span><br><span class="line">        </span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p><code>distinct()去除重复元素</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Stream.concat(list1.stream(),list.stream()).distinct().forEach(System.out::println);<span class="comment">////去除</span></span><br><span class="line"><span class="comment">//        刘一</span></span><br><span class="line"><span class="comment">//        陈二</span></span><br><span class="line"><span class="comment">//        孙七</span></span><br><span class="line"><span class="comment">//        周八</span></span><br><span class="line"><span class="comment">//        张三</span></span><br><span class="line"><span class="comment">//        李四</span></span><br><span class="line"><span class="comment">//        王五</span></span><br><span class="line"><span class="comment">//        钱六</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p><code>sorted()按自然排序排序流中的元素</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">list.stream().sorted().forEach(System.out::println);</span><br><span class="line"><span class="comment">//        张三</span></span><br><span class="line"><span class="comment">//        李四</span></span><br><span class="line"><span class="comment">//        王五</span></span><br><span class="line"><span class="comment">//        钱六</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p><code>sorted(Comparator comparator)按比较器排序流中的元素</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">list.stream().sorted(<span class="keyword">new</span> Comparator&lt;String&gt;() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(String o1, String o2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> o2.compareTo(o1);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).forEach(System.out::println);</span><br><span class="line"><span class="comment">//        钱六</span></span><br><span class="line"><span class="comment">//        王五</span></span><br><span class="line"><span class="comment">//        李四</span></span><br><span class="line"><span class="comment">//        张三</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><code>map(Function func)按照函数的映射关系操作，结果存储在返回的流中</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">ArrayList&lt;String&gt; list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">list.add(<span class="string">&quot;0&quot;</span>);</span><br><span class="line">list.add(<span class="string">&quot;1&quot;</span>);</span><br><span class="line">list.add(<span class="string">&quot;2&quot;</span>);</span><br><span class="line">list.add(<span class="string">&quot;3&quot;</span>);</span><br><span class="line">list.stream().map(Integer::parseInt).forEach(System.out::println);</span><br><span class="line"><span class="comment">//        0</span></span><br><span class="line"><span class="comment">//        1</span></span><br><span class="line"><span class="comment">//        2</span></span><br><span class="line"><span class="comment">//        3</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><code>mapToInt(ToIntFunction mapper)该方法返回的流为IntStream</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">list.stream().mapToInt(Integer::parseInt).forEach(System.out::println);</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>sum()返回数值，结束流的操作</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> result = list.stream().mapToInt(Integer::parseInt).sum();</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>collect(Collector collector将流转化为Collection</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List&lt;String&gt; s =  list.stream().collect(Collectors.toList());</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="异常"><a href="#异常" class="headerlink" title="异常"></a>异常</h1><h2 id="异常体系"><a href="#异常体系" class="headerlink" title="异常体系"></a>异常体系</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">graph TD</span><br><span class="line">Trowable--&gt;Eorror --&gt;问题严重,处理不了</span><br><span class="line">Trowable--&gt;Exception</span><br><span class="line">Exception--&gt;RumtimeException --&gt;可以预见的异常,在运行时处理</span><br><span class="line">Exception--&gt;非RumtimeException --&gt;编译前就必须处理,编译器会自动检测</span><br></pre></td></tr></table></figure>
<h2 id="异常处理"><a href="#异常处理" class="headerlink" title="异常处理"></a>异常处理</h2><ul>
<li>如果不人为处理，<code>Jvm</code>会默认处理<ul>
<li>抛出异常，给出异常原因</li>
<li>停止运行程序</li>
</ul>
</li>
<li>人为处理</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    <span class="comment">//可能会发生异常的代码</span></span><br><span class="line">&#125;<span class="keyword">catch</span>(ExceptionType e)&#123;</span><br><span class="line">    e.printStackTrace();<span class="comment">//打印异常信息</span></span><br><span class="line">    <span class="comment">//处理发生异常</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[]  a = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(a[<span class="number">3</span>]);</span><br><span class="line">        &#125;<span class="keyword">catch</span> (ArrayIndexOutOfBoundsException e)&#123;</span><br><span class="line">            </span><br><span class="line">            System.out.println(e.getMessage());</span><br><span class="line">            System.out.println(e.toString());</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;结束&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出:</span></span><br><span class="line"><span class="comment">=====================</span></span><br><span class="line"><span class="comment">Index 3 out of bounds for length 3</span></span><br><span class="line"><span class="comment">java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3</span></span><br><span class="line"><span class="comment">=====================</span></span><br><span class="line"><span class="comment">结束</span></span><br><span class="line"><span class="comment">java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3</span></span><br><span class="line"><span class="comment">	at test.Main.main(Main.java:15)</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">Process finished with exit code 0                                        </span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>​    java的异常提示包括两部分:    </p>
<ol>
<li>异常类型</li>
<li>异常信息</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">e.getMessage() -&gt;<span class="string">&quot;Index 3 out of bounds for length 3&quot;</span></span><br><span class="line">e.toString() -&gt; <span class="string">&quot;java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3&quot;</span></span><br><span class="line">e.printStackTrace() -&gt; </span><br><span class="line">    <span class="string">&quot;java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 at test.Main.main(Main.java:15)&quot;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>当无权限处理或不知道如何处理时可用<code>throws</code>抛出异常，留给方法调用者处理</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> <span class="keyword">throws</span> FileNotFoundException </span>&#123;</span><br><span class="line">    File file;</span><br><span class="line">    FileReader fileReader = <span class="keyword">new</span> FileReader(<span class="string">&quot;test.png&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="自定义异常"><a href="#自定义异常" class="headerlink" title="自定义异常"></a>自定义异常</h2><p>自定义异常需要继承<code>Exception</code>或<code>RumtimeException</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//自定义异常</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestException</span> <span class="keyword">extends</span> <span class="title">Exception</span></span>&#123;</span><br><span class="line">    TestException()&#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    TestException(String message)&#123;</span><br><span class="line">        <span class="keyword">super</span>(message);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//使用自定义异常</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            test();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (TestException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> <span class="keyword">throws</span> TestException </span>&#123;</span><br><span class="line">        <span class="comment">//code ...</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> TestException(<span class="string">&quot;测试代码有问题!&quot;</span>);</span><br><span class="line">        &#125;         </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">est.TestException: 测试代码有问题!</span></span><br><span class="line"><span class="comment">	at test.Main.test(Main.java:23)</span></span><br><span class="line"><span class="comment">	at test.Main.main(Main.java:14)</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h1 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h1><h2 id="单列存储结构Collection"><a href="#单列存储结构Collection" class="headerlink" title="单列存储结构Collection"></a>单列存储结构Collection</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">classDiagram</span><br><span class="line">	class Iterator~T~&#123;</span><br><span class="line">		&lt;&lt;interface&gt;&gt;</span><br><span class="line">		hasNext() boolean</span><br><span class="line">		next() ~T~</span><br><span class="line">		remove()</span><br><span class="line">	&#125;</span><br><span class="line">	class Iterable~T~&#123;</span><br><span class="line">		&lt;&lt;interface&gt;&gt;</span><br><span class="line">		+iterator() Iterator~T~</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	class Collection~T~&#123;</span><br><span class="line">		  size() int</span><br><span class="line">		  isEmpty() boolean</span><br><span class="line">		  contains(Object) boolean</span><br><span class="line">          iterator() Iterator~E~</span><br><span class="line">          toArray(T[]) T[]</span><br><span class="line">          toArray() Object[]</span><br><span class="line">          add(T) boolean</span><br><span class="line">          remove(Object) boolean</span><br><span class="line">		  containsAll(Collection~T~) boolean</span><br><span class="line">          addAll(Collection~T~) boolean</span><br><span class="line">          removeAll(Collection~T~) boolean</span><br><span class="line">          retainAll(Collection~T~) boolean</span><br><span class="line">          clear() void</span><br><span class="line">          equals(Object) boolean</span><br><span class="line">          stream() Stream&lt;E&gt;</span><br><span class="line">          parallelStream() Stream&lt;E&gt; </span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">Iterator &lt;-- Iterable</span><br><span class="line">Iterable &lt;|-- Collection</span><br><span class="line">Collection&lt;|--List</span><br><span class="line">Collection&lt;|--Set</span><br><span class="line">Collection&lt;|--Queue</span><br><span class="line">Collection&lt;|--Stack</span><br></pre></td></tr></table></figure>
<h3 id="Set"><a href="#Set" class="headerlink" title="Set"></a>Set</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">classDiagram</span><br><span class="line">class Collection</span><br><span class="line">&lt;&lt;interface&gt;&gt; Collection</span><br><span class="line"></span><br><span class="line">class AbstractCollection</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractCollection</span><br><span class="line">Collection &lt;|.. AbstractCollection</span><br><span class="line"></span><br><span class="line">class Set</span><br><span class="line">&lt;&lt;interface&gt;&gt; Set</span><br><span class="line">Collection &lt;|-- Set</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class AbstractSet</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractSet</span><br><span class="line">AbstractCollection &lt;|-- AbstractSet</span><br><span class="line">Set &lt;|.. AbstractSet</span><br><span class="line"></span><br><span class="line">class TreeSet</span><br><span class="line">AbstractSet &lt;|-- TreeSet</span><br><span class="line">NavigableSet &lt;|.. TreeSet</span><br><span class="line"></span><br><span class="line">class HashSet</span><br><span class="line">AbstractSet &lt;|-- HashSet</span><br><span class="line">Set &lt;|.. HashSet</span><br><span class="line"></span><br><span class="line">class SortedSet</span><br><span class="line">&lt;&lt;interface&gt;&gt; SortedSet</span><br><span class="line">Set &lt;|-- SortedSet</span><br><span class="line"></span><br><span class="line">class NavigableSet</span><br><span class="line">&lt;&lt;interface&gt;&gt; NavigableSet</span><br><span class="line">SortedSet &lt;|-- NavigableSet</span><br><span class="line"></span><br><span class="line">class LinkedHashSet</span><br><span class="line">HashSet &lt;|-- LinkedHashSet</span><br><span class="line"></span><br><span class="line">class EnumSet</span><br><span class="line">&lt;&lt;abstract&gt;&gt; EnumSet</span><br><span class="line">AbstractSet &lt;|-- EnumSet</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="List"><a href="#List" class="headerlink" title="List"></a>List</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">classDiagram</span><br><span class="line">class Collection</span><br><span class="line">&lt;&lt;interface&gt;&gt; Collection</span><br><span class="line"></span><br><span class="line">class AbstractCollection</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractCollection</span><br><span class="line">Collection &lt;|.. AbstractCollection</span><br><span class="line"></span><br><span class="line">class List</span><br><span class="line">&lt;&lt;interface&gt;&gt; List</span><br><span class="line">Collection &lt;|-- List</span><br><span class="line"></span><br><span class="line">class AbstractList</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractList</span><br><span class="line">AbstractCollection &lt;|-- AbstractList</span><br><span class="line">List &lt;|.. AbstractList</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class ArrayList</span><br><span class="line">AbstractList &lt;|-- ArrayList</span><br><span class="line">List &lt;|.. ArrayList</span><br><span class="line"></span><br><span class="line">class AbstractSequentialList</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractSequentialList</span><br><span class="line">AbstractList &lt;|-- AbstractSequentialList</span><br><span class="line"></span><br><span class="line">class Deque</span><br><span class="line">&lt;&lt;interface&gt;&gt; Deque</span><br><span class="line"></span><br><span class="line">class LinkedList</span><br><span class="line">AbstractSequentialList &lt;|-- LinkedList</span><br><span class="line">List &lt;|.. LinkedList</span><br><span class="line">Deque &lt;|.. LinkedList</span><br><span class="line"></span><br><span class="line">class Vector</span><br><span class="line">AbstractList &lt;|-- Vector</span><br><span class="line">List &lt;|.. Vector</span><br><span class="line"></span><br><span class="line">class Stack</span><br><span class="line">Vector &lt;|-- Stack</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="Queue"><a href="#Queue" class="headerlink" title="Queue"></a>Queue</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">classDiagram</span><br><span class="line">class Collection</span><br><span class="line">&lt;&lt;interface&gt;&gt; Collection</span><br><span class="line"></span><br><span class="line">class AbstractCollection</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractCollection</span><br><span class="line">Collection &lt;|.. AbstractCollection</span><br><span class="line"></span><br><span class="line">class Queue</span><br><span class="line">&lt;&lt;interface&gt;&gt; Queue</span><br><span class="line">Collection &lt;|-- Queue</span><br><span class="line"></span><br><span class="line">class Deque</span><br><span class="line">&lt;&lt;interface&gt;&gt; Deque</span><br><span class="line">Queue &lt;|-- Deque</span><br><span class="line"></span><br><span class="line">class BlockingQueue</span><br><span class="line">&lt;&lt;interface&gt;&gt; BlockingQueue</span><br><span class="line">Queue &lt;|-- BlockingQueue</span><br><span class="line"></span><br><span class="line">class BlockingDeque</span><br><span class="line">&lt;&lt;interface&gt;&gt; BlockingDeque</span><br><span class="line">BlockingQueue &lt;|-- BlockingDeque</span><br><span class="line">Deque &lt;|-- BlockingDeque</span><br><span class="line"></span><br><span class="line">class AbstractQueue</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractQueue</span><br><span class="line">AbstractCollection &lt;|--AbstractQueue</span><br><span class="line">Queue&lt;|..AbstractQueue</span><br><span class="line"></span><br><span class="line">class LinkedTransferQueue</span><br><span class="line">AbstractQueue &lt;|--LinkedTransferQueue</span><br><span class="line">TransferQueue &lt;|..LinkedTransferQueue</span><br><span class="line"></span><br><span class="line">class TransferQueue</span><br><span class="line">&lt;&lt;interface&gt;&gt; TransferQueue</span><br><span class="line">BlockingQueue &lt;|-- TransferQueue</span><br><span class="line"></span><br><span class="line">class SynchronousQueue</span><br><span class="line">AbstractQueue &lt;|-- SynchronousQueue</span><br><span class="line">BlockingQueue &lt;|.. SynchronousQueue</span><br><span class="line"></span><br><span class="line">class PriorityQueue</span><br><span class="line">AbstractQueue &lt;|-- PriorityQueue</span><br><span class="line"></span><br><span class="line">class LinkedBlockingQueue</span><br><span class="line">AbstractQueue &lt;|-- LinkedBlockingQueue</span><br><span class="line">BlockingQueue &lt;|.. LinkedBlockingQueue</span><br><span class="line"></span><br><span class="line">class PriorityBlockingQueue</span><br><span class="line">AbstractQueue &lt;|-- PriorityBlockingQueue</span><br><span class="line">BlockingQueue &lt;|.. PriorityBlockingQueue</span><br><span class="line"></span><br><span class="line">class ArrayBlockingQueue</span><br><span class="line">AbstractQueue &lt;|-- ArrayBlockingQueue</span><br><span class="line">BlockingQueue &lt;|.. ArrayBlockingQueue</span><br><span class="line"></span><br><span class="line">class LinkedBlockingDeque</span><br><span class="line">AbstractQueue &lt;|--LinkedBlockingDeque</span><br><span class="line">BlockingDeque &lt;|..LinkedBlockingDeque</span><br><span class="line"></span><br><span class="line">class DelayQueue</span><br><span class="line">AbstractQueue&lt;|--DelayQueue</span><br><span class="line">BlockingQueue&lt;|..DelayQueue</span><br><span class="line"></span><br><span class="line">class LinkedList</span><br><span class="line">Deque&lt;|--LinkedList</span><br><span class="line"></span><br><span class="line">class ArrayDeque</span><br><span class="line">Deque&lt;|--ArrayDeque</span><br><span class="line"></span><br><span class="line">class ConcurrentLinkedQueue</span><br><span class="line">AbstractQueue&lt;|--ConcurrentLinkedQueue</span><br><span class="line">Queue&lt;|--ConcurrentLinkedQueue</span><br></pre></td></tr></table></figure>
<h3 id="Stack"><a href="#Stack" class="headerlink" title="Stack"></a>Stack</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">classDiagram</span><br><span class="line">class Collection</span><br><span class="line">&lt;&lt;interface&gt;&gt; Collection</span><br><span class="line"></span><br><span class="line">class AbstractCollection</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractCollection</span><br><span class="line">Collection &lt;|.. AbstractCollection</span><br><span class="line"></span><br><span class="line">class List</span><br><span class="line">&lt;&lt;interface&gt;&gt; List</span><br><span class="line">Collection &lt;|-- List</span><br><span class="line"></span><br><span class="line">class AbstractList</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractList</span><br><span class="line">AbstractCollection &lt;|-- AbstractList</span><br><span class="line">List &lt;|.. AbstractList</span><br><span class="line"></span><br><span class="line">class Vector</span><br><span class="line">AbstractList &lt;|-- Vector</span><br><span class="line">List &lt;|.. Vector</span><br><span class="line"></span><br><span class="line">class Stack</span><br><span class="line">Vector &lt;|-- Stack</span><br></pre></td></tr></table></figure>
<h2 id="双列存储结构Map"><a href="#双列存储结构Map" class="headerlink" title="双列存储结构Map"></a>双列存储结构Map</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">classDiagram</span><br><span class="line">class Map</span><br><span class="line">&lt;&lt;interface&gt;&gt; Map</span><br><span class="line"></span><br><span class="line">class PropertyHashMap</span><br><span class="line">Map&lt;|..PropertyHashMap</span><br><span class="line"></span><br><span class="line">class WeakIdentityHashMap</span><br><span class="line">Map&lt;|..WeakIdentityHashMap</span><br><span class="line"></span><br><span class="line">class AbstractMap</span><br><span class="line">&lt;&lt;abstract&gt;&gt; AbstractMap</span><br><span class="line">Map &lt;|.. AbstractMap</span><br><span class="line"></span><br><span class="line">class IdentityHashMap</span><br><span class="line">AbstractMap&lt;|--IdentityHashMap</span><br><span class="line">Map&lt;|..IdentityHashMap</span><br><span class="line"></span><br><span class="line">class SortedMap</span><br><span class="line">&lt;&lt;interface&gt;&gt; SortedMap</span><br><span class="line">Map&lt;|--SortedMap</span><br><span class="line"></span><br><span class="line">class NavigableMap</span><br><span class="line">&lt;&lt;interface&gt;&gt; NavigableMap</span><br><span class="line">SortedMap&lt;|--NavigableMap</span><br><span class="line"></span><br><span class="line">class TreeMap</span><br><span class="line">AbstractMap &lt;|-- TreeMap</span><br><span class="line">NavigableMap &lt;|.. TreeMap</span><br><span class="line"></span><br><span class="line">class HashMap</span><br><span class="line">AbstractMap&lt;|--HashMap</span><br><span class="line">Map&lt;|..HashMap</span><br><span class="line"></span><br><span class="line">class LinkedHashMap</span><br><span class="line">HashMap&lt;|--LinkedHashMap</span><br><span class="line">Map&lt;|..LinkedHashMap</span><br><span class="line"></span><br><span class="line">class HashTable</span><br><span class="line">Dictionary&lt;|--HashTable</span><br><span class="line">Map&lt;..HashTable</span><br><span class="line"></span><br><span class="line">class ConcurrentMap</span><br><span class="line">&lt;&lt;interface&gt;&gt; ConcurrentMap</span><br><span class="line">Map&lt;|--ConcurrentMap</span><br><span class="line"></span><br><span class="line">class ConcurrentHashMap</span><br><span class="line">ConcurrentMap&lt;|..ConcurrentHashMap</span><br><span class="line">AbstractMap&lt;|--ConcurrentHashMap</span><br></pre></td></tr></table></figure>
<h2 id="常用集合类"><a href="#常用集合类" class="headerlink" title="常用集合类"></a>常用集合类</h2><ul>
<li><code>ArrayList</code></li>
<li><code>LinkedList</code></li>
<li><code>ArrayDeque</code></li>
<li><code>PriorityQueue</code></li>
<li><code>HashSet</code></li>
<li><code>HashMap</code></li>
<li><code>HashTable</code></li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">graph TD</span><br><span class="line">集合--&gt;Collection</span><br><span class="line">集合--&gt;Map</span><br><span class="line"></span><br><span class="line">Collection--&gt;List</span><br><span class="line"></span><br><span class="line">Collection--&gt;Queue</span><br><span class="line">Collection--&gt;Set</span><br><span class="line"></span><br><span class="line">Map--&gt;HashMap</span><br><span class="line">Map--&gt;TreeMap</span><br><span class="line">Map--&gt;HashTable</span><br><span class="line">Map--&gt;ConcurrentHashMap</span><br><span class="line">HashMap--&gt;LinkedHashMap</span><br><span class="line"></span><br><span class="line">Queue--&gt;Deque</span><br><span class="line">Queue--&gt;PriorityQueue</span><br><span class="line">Queue--&gt;ConcurrentLinkedQueue</span><br><span class="line">Deque--&gt;LinkedList</span><br><span class="line">Deque--&gt;ArrayDeque</span><br><span class="line"></span><br><span class="line">List--&gt;ArrayList</span><br><span class="line">List--&gt;Vector--&gt;Stack</span><br><span class="line">List--&gt;LinkedList</span><br><span class="line"></span><br><span class="line">Set--&gt;HashSet--&gt;LinkedHashSet</span><br><span class="line">Set--&gt;TreeSet</span><br></pre></td></tr></table></figure>
<h3 id="Collection常用方法"><a href="#Collection常用方法" class="headerlink" title="Collection常用方法"></a>Collection常用方法</h3><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:left">方法</th>
<th style="text-align:left">方法描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>boolean add(E e)</code></td>
<td style="text-align:left">添加一个元素</td>
</tr>
<tr>
<td style="text-align:left"><code>boolean remove(Object o)</code></td>
<td style="text-align:left">删除集合中指定元素<code>o</code></td>
</tr>
<tr>
<td style="text-align:left"><code>void clear()</code></td>
<td style="text-align:left">删除集合中所有元素</td>
</tr>
<tr>
<td style="text-align:left"><code>boolen isEmpty()</code></td>
<td style="text-align:left">判断集合是否为空</td>
</tr>
<tr>
<td style="text-align:left"><code>int size()</code></td>
<td style="text-align:left">返回集合中元素个数</td>
</tr>
<tr>
<td style="text-align:left"><code>boolean contains(Object o)</code></td>
<td style="text-align:left">判断集合中是否包含元素<code>o</code></td>
</tr>
<tr>
<td style="text-align:left"><code>Iterator iterator()</code></td>
<td style="text-align:left">返回此集合中的元素的迭代器。</td>
</tr>
<tr>
<td style="text-align:left"><code>default stream()</code></td>
<td style="text-align:left">返回以此集合作为源的顺序 <code>Stream</code> 。</td>
</tr>
<tr>
<td style="text-align:left"><code>Object toArray()</code></td>
<td style="text-align:left">返回一个包含此集合中所有元素的数组。</td>
</tr>
<tr>
<td style="text-align:left"><code>boolean addAll(Collection&lt;? extends E&gt; c)</code></td>
<td style="text-align:left">将集合<code>c</code>中的所有元素添加到此集合。</td>
</tr>
<tr>
<td style="text-align:left"><code>boolean containsAll(Collection&lt;?&gt; c)</code></td>
<td style="text-align:left">判断集合<code>c</code>是否为此集合的字迹</td>
</tr>
<tr>
<td style="text-align:left"><code>boolean removeAll(Collection&lt;?&gt; c)</code></td>
<td style="text-align:left">删除此集合与集合<code>c</code>的交集</td>
</tr>
</tbody>
</table>
</div>
<h3 id="Collection的遍历"><a href="#Collection的遍历" class="headerlink" title="Collection的遍历"></a>Collection的遍历</h3><ul>
<li><p>使用迭代器遍历</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Collection&lt;String&gt; collection = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">collection.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">collection.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">collection.add(<span class="string">&quot;javaee&quot;</span>);</span><br><span class="line"></span><br><span class="line">Iterator&lt;String&gt; iterator = collection.iterator();</span><br><span class="line"><span class="keyword">while</span> (iterator.hasNext())&#123;</span><br><span class="line">    String s = iterator.next();</span><br><span class="line">    System.out.println(s);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>使用增强<code>for</code>循环遍历</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (String s : collection)&#123;</span><br><span class="line">    System.out.println(s);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>并发修改异常</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Iterator&lt;String&gt; iterator = collection.iterator();</span><br><span class="line"><span class="keyword">while</span> (iterator.hasNext())&#123;</span><br><span class="line">    String next = iterator.next();</span><br><span class="line">    collection.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    System.out.println(next);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (String s : collection)&#123;</span><br><span class="line">    System.out.println(s);</span><br><span class="line">    collection.add(<span class="string">&quot;ppt&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Exception in thread <span class="string">&quot;main&quot;</span> java.util.ConcurrentModificationException</span><br><span class="line">	at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:<span class="number">1013</span>)</span><br><span class="line">	at java.base/java.util.ArrayList$Itr.next(ArrayList.java:<span class="number">967</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<blockquote>
<p>注:使用迭代器遍历集合时增减集合元素会造成并发修改异常,增强<code>for</code>循环的本质也是迭代器遍历，所以同样也会造成该异常</p>
</blockquote>
<h4 id="List-1"><a href="#List-1" class="headerlink" title="List"></a>List</h4><h5 id="特点："><a href="#特点：" class="headerlink" title="特点："></a>特点：</h5><ul>
<li>有序</li>
<li>可重复</li>
<li>可向指定位置插入元素</li>
<li>可访问指定位置元素</li>
</ul>
<h5 id="常用方法"><a href="#常用方法" class="headerlink" title="常用方法"></a>常用方法</h5><div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>方法描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>void add(int index,E e)</code></td>
<td>在指定位置添加元素</td>
</tr>
<tr>
<td><code>boolean add(E e)</code></td>
<td>添加一个元素到末尾</td>
</tr>
<tr>
<td><code>E remove(int index)</code></td>
<td>删除指定位置元素</td>
</tr>
<tr>
<td><code>E set(int index,E e)</code></td>
<td>修改指定位置的元素,返回被修改的元素</td>
</tr>
<tr>
<td><code>E get(int index)</code></td>
<td>返回指定位置的元素</td>
</tr>
</tbody>
</table>
</div>
<h4 id="Set-1"><a href="#Set-1" class="headerlink" title="Set"></a>Set</h4><h5 id="特点"><a href="#特点" class="headerlink" title="特点:"></a>特点:</h5><ul>
<li>无序</li>
<li>不重复</li>
</ul>
<h5 id="常用方法-1"><a href="#常用方法-1" class="headerlink" title="常用方法"></a>常用方法</h5><div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>方法描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>boolean add(E e)</code></td>
<td>添加一个元素</td>
</tr>
<tr>
<td><code>boolean contains(Object o)</code></td>
<td>判断元素是否存在</td>
</tr>
<tr>
<td><code>int size()</code></td>
<td>获取集合中元素的个数</td>
</tr>
<tr>
<td><code>boolean remove(Object o)</code></td>
<td>移除某个元素</td>
</tr>
<tr>
<td><code>void clear()</code></td>
<td>清空集合</td>
</tr>
<tr>
<td><code>boolean isEmpty()</code></td>
<td>判断集合是否为空</td>
</tr>
</tbody>
</table>
</div>
<h4 id="Queue-1"><a href="#Queue-1" class="headerlink" title="Queue"></a>Queue</h4><h5 id="特点-1"><a href="#特点-1" class="headerlink" title="特点:"></a>特点:</h5><ul>
<li>只允许在端操作，不允许在队列的中间操作</li>
<li>有序</li>
</ul>
<h5 id="常用方法-2"><a href="#常用方法-2" class="headerlink" title="常用方法"></a>常用方法</h5><ul>
<li>单端队列</li>
</ul>
<div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>方法描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>E element()</code></td>
<td>返回首部元素，如果为空，抛出异常</td>
</tr>
<tr>
<td><code>E remove()</code></td>
<td>返回并移除首部元素，如果为空，抛出异常</td>
</tr>
<tr>
<td><code>E peek()</code></td>
<td>返回首部元素，如果为空，返回<code>null</code></td>
</tr>
<tr>
<td><code>E poll()</code></td>
<td>返回并移除首部元素，如果为空，返回<code>null</code></td>
</tr>
<tr>
<td><code>boolean add(E e)</code></td>
<td>添加一个元素在队列尾部</td>
</tr>
</tbody>
</table>
</div>
<ul>
<li>双端队列</li>
</ul>
<div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>方法描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>E element()</code></td>
<td>返回首部元素，如果为空，抛出异常</td>
</tr>
<tr>
<td><code>void addFirst(E e)</code></td>
<td>添加一个元素在队列首部</td>
</tr>
<tr>
<td><code>void addLast(E e)</code></td>
<td>添加一个元素在队列尾部</td>
</tr>
<tr>
<td><code>E removeFirst()</code></td>
<td>返回并移除首部元素，如果为空，抛出异常</td>
</tr>
<tr>
<td><code>E removeLast()</code></td>
<td>返回并移除尾部元素，如果为空，抛出异常</td>
</tr>
<tr>
<td><code>E peekFirst()</code></td>
<td>返回首部元素，如果为空，返回<code>null</code></td>
</tr>
<tr>
<td><code>E peekLast()</code></td>
<td>返回尾部首部元素，如果为空，返回<code>null</code></td>
</tr>
<tr>
<td><code>E pollFirst()</code></td>
<td>返回并移除首部元素，如果为空，返回<code>null</code></td>
</tr>
<tr>
<td><code>E pollLast()</code></td>
<td>返回并移除尾部元素，如果为空，返回<code>null</code></td>
</tr>
<tr>
<td><code>E poll()</code></td>
<td>等价于<code>pollFirst()</code></td>
</tr>
<tr>
<td><code>E peek()</code></td>
<td>等价于<code>peekFirst()</code></td>
</tr>
<tr>
<td><code>E push()</code></td>
<td>等价于<code>addFirst()</code></td>
</tr>
<tr>
<td><code>E pop()</code></td>
<td>等价于<code>removeFirst()</code></td>
</tr>
</tbody>
</table>
</div>
<h4 id="Stack-1"><a href="#Stack-1" class="headerlink" title="Stack"></a>Stack</h4><h5 id="特点：-1"><a href="#特点：-1" class="headerlink" title="特点："></a>特点：</h5><ul>
<li><p>只允许操作栈顶元素，插入只能插入到栈顶，删除也只能删除栈顶元素</p>
</li>
<li><p>有序</p>
<p>| 方法 | 方法描述 |<br>| —— | —— |<br>| <code>E push()</code>       | 向栈顶添加元素一个元素 |<br>| <code>E peek()</code> | 查看栈顶元素，为空抛出异常 |<br>| <code>E pop()</code> | 返回并删除栈顶元素，为空抛出异常 |</p>
<p>常用双端队列代替栈，因为在<code>java</code>中该方法加锁，效率较低</p>
</li>
</ul>
<h3 id="Map常用方法"><a href="#Map常用方法" class="headerlink" title="Map常用方法"></a>Map常用方法</h3><div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>方法描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>V put(K key,V value)</code></td>
<td>向<code>map</code>中添加一个键值对</td>
</tr>
<tr>
<td><code>V get(K key)</code></td>
<td>获取<code>map</code>中键为<code>key</code>的值</td>
</tr>
<tr>
<td><code>void clear()</code></td>
<td>清空集合</td>
</tr>
<tr>
<td><code>V remove(Object key)</code></td>
<td>删除<code>map</code>中键为<code>key</code>的键值对</td>
</tr>
<tr>
<td><code>V replace(Object key,Object V)</code></td>
<td>将键为<code>key</code>的值进行替换</td>
</tr>
<tr>
<td><code>Set keySet()</code></td>
<td>获得键的集合</td>
</tr>
<tr>
<td><code>Collection&lt;V&gt; values()</code></td>
<td>获得值的集合</td>
</tr>
<tr>
<td><code>Set&lt;Map.Entry&lt;K, V&gt;&gt; entrySet()</code></td>
<td>获得键值对的集合实体（用于遍历）</td>
</tr>
<tr>
<td><code>boolean containsKey(Object key)</code></td>
<td>判断是否存在该键</td>
</tr>
<tr>
<td><code>boolean containsValue(Object value)</code></td>
<td>判断是否存在该值</td>
</tr>
</tbody>
</table>
</div>
<h3 id="Map的遍历"><a href="#Map的遍历" class="headerlink" title="Map的遍历"></a>Map的遍历</h3><ul>
<li>使用键遍历</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;String,String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">Set&lt;String&gt; strings = map.keySet();</span><br><span class="line"><span class="keyword">for</span> (String s:strings)&#123;</span><br><span class="line">    System.out.println(map.get(s));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>使用集合实体遍历</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Map&lt;String,String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">Set&lt;Map.Entry&lt;String, String&gt;&gt; entries = map.entrySet();</span><br><span class="line"><span class="keyword">for</span> (Map.Entry&lt;String,String&gt; entry: entries)&#123;</span><br><span class="line">    System.out.println(entry.getValue());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="IO流"><a href="#IO流" class="headerlink" title="IO流"></a>IO流</h1><h2 id="File"><a href="#File" class="headerlink" title="File"></a>File</h2><div class="table-container">
<table>
<thead>
<tr>
<th>构造方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>File(File parent, String child)</code></td>
<td>从父抽象路径名和子路径名字符串创建新的<code>File</code>实例。</td>
</tr>
<tr>
<td><code>File(String pathname)</code></td>
<td>通过将给定的路径名字符串转换为抽象路径名来创建新的<code>File</code>实例。</td>
</tr>
<tr>
<td><code>File(String parent,  String child)</code></td>
<td>从父路径名字符串和子路径名字符串创建新的<code>File</code>实例。</td>
</tr>
</tbody>
</table>
</div>
<h4 id="创建File对象"><a href="#创建File对象" class="headerlink" title="创建File对象"></a>创建<code>File</code>对象</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">File file = <span class="keyword">new</span> File(<span class="string">&quot;test.txt&quot;</span>);</span><br></pre></td></tr></table></figure>
<h4 id="对象的使用"><a href="#对象的使用" class="headerlink" title="对象的使用"></a>对象的使用</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//如果该目录下有test.txt返回false,如果没有，则创建文件,并返回true</span></span><br><span class="line">File file0 = <span class="keyword">new</span> File(<span class="string">&quot;D:/test.txt&quot;</span>);</span><br><span class="line">System.out.println(file0.createNewFile());</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果有该目录并且该目录没有该文件，则创建文件并返回true否则抛出异常 `java.io.IOException: 系统找不到指定的路径。`</span></span><br><span class="line">File file1 = <span class="keyword">new</span> File(<span class="string">&quot;D:/Hello/test.txt&quot;</span>);</span><br><span class="line">System.out.println(file1.createNewFile());</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果没有该目录且父目录存在.则创建目录返回true,否则返回false</span></span><br><span class="line">File file2 = <span class="keyword">new</span> File(<span class="string">&quot;D:/World&quot;</span>);</span><br><span class="line">System.out.println(file2.mkdir());</span><br><span class="line"></span><br><span class="line"><span class="comment">//如果目录目录存在，则返回false,不存在则递归创建目录</span></span><br><span class="line">File file3 = <span class="keyword">new</span> File(<span class="string">&quot;D:/Hello/World&quot;</span>);</span><br><span class="line">System.out.println(file3.mkdirs());</span><br><span class="line"></span><br><span class="line"><span class="comment">//该方法会装甲world.txt的目录，创建文件和目录只和调用的方法有关和文件路径无关</span></span><br><span class="line">File file4 = <span class="keyword">new</span> File(<span class="string">&quot;D:/Hello/world.txt&quot;</span>);</span><br><span class="line">System.out.println(file4.mkdir());</span><br></pre></td></tr></table></figure>
<h4 id="File类常用方法"><a href="#File类常用方法" class="headerlink" title="File类常用方法"></a>File类常用方法</h4><div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>boolean isDirectory()</code></td>
<td>判断是否为目录</td>
</tr>
<tr>
<td><code>boolean isFile()</code></td>
<td>判断是否为文件</td>
</tr>
<tr>
<td><code>boolean exists()</code></td>
<td>判断文件是否存在</td>
</tr>
<tr>
<td><code>String getAbsolutePath()</code></td>
<td>获取绝对路径</td>
</tr>
<tr>
<td><code>String getParant()</code></td>
<td>获取父路径</td>
</tr>
<tr>
<td><code>String getName()</code></td>
<td>获取文件名</td>
</tr>
<tr>
<td><code>String[] list()</code></td>
<td>获取抽象路径所表示的目录下所有文件或目录的名称组成的数组</td>
</tr>
<tr>
<td><code>File[] listFiles()</code></td>
<td>获取抽象路径所表示的目录下所有文件或目录的<code>File</code>对象</td>
</tr>
<tr>
<td><code>boolean delete()</code></td>
<td>删除抽象路径所表示的文件或目录，当抽象路径表示目录时，必须先删除抽象路径下的文件才能删除该目录</td>
</tr>
</tbody>
</table>
</div>
<h2 id="IO流常用体系"><a href="#IO流常用体系" class="headerlink" title="IO流常用体系"></a>IO流常用体系</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">graph LR</span><br><span class="line">IO流 --&gt; 字符流  &amp; 字节流</span><br><span class="line">字符流 --&gt; Reader &amp; Writer</span><br><span class="line">Reader --&gt; BufferedReader &amp; InputStreamReader</span><br><span class="line">Writer --&gt; BufferedWriter &amp; OutputStreamWriter</span><br><span class="line">InputStreamReader --&gt; FileReader</span><br><span class="line">OutputStreamWriter --&gt; FileWriter</span><br><span class="line"></span><br><span class="line">字节流 --&gt; InputStream &amp; OutputStream</span><br><span class="line">InputStream --&gt; FileInputStream &amp; FilterInputStream &amp; ObjectInputStream</span><br><span class="line">OutputStream --&gt; FileOutputStream &amp; FilterOutputStream &amp; ObjectOutputStream</span><br><span class="line">FilterInputStream --&gt; BufferedInputStream &amp; DataInputStream </span><br><span class="line">FilterOutputStream --&gt; BufferedOutputStream &amp; DataOutputStream </span><br></pre></td></tr></table></figure>
<h2 id="字节流"><a href="#字节流" class="headerlink" title="字节流"></a>字节流</h2><h3 id="字节流写入数据"><a href="#字节流写入数据" class="headerlink" title="字节流写入数据"></a>字节流写入数据</h3><ul>
<li><p>覆盖写</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">FileOutputStream fos = <span class="keyword">new</span> FileOutputStream(<span class="string">&quot;test.txt&quot;</span>);</span><br><span class="line">fos.write(<span class="string">&quot;test&quot;</span>.getBytes());<span class="comment">//windows 写入换行 &quot;\r\n&quot;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>追加写</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">FileOutputStream fos = <span class="keyword">new</span> FileOutputStream(<span class="string">&quot;test.txt&quot;</span>,<span class="keyword">true</span>);</span><br><span class="line">fos.write(<span class="string">&quot;test&quot;</span>.getBytes());</span><br></pre></td></tr></table></figure>
</li>
<li><p>每次写一个字节</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">write</span><span class="params">(<span class="keyword">int</span> b)</span></span>;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fos.write(<span class="number">97</span>);<span class="comment">//write &#x27;a&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>每次写多个字节</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">write</span><span class="params">(<span class="keyword">byte</span> b[])</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">write</span><span class="params">(<span class="keyword">byte</span> b[], <span class="keyword">int</span> off, <span class="keyword">int</span> len)</span></span>;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">fos.write(<span class="string">&quot;test&quot;</span>.getBytes());<span class="comment">//write &quot;test&quot;</span></span><br><span class="line">fos.write(<span class="string">&quot;test&quot;</span>.getBytest(),<span class="number">1</span>,<span class="number">3</span>);<span class="comment">//write &quot;est&quot;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="字节流读数据"><a href="#字节流读数据" class="headerlink" title="字节流读数据"></a>字节流读数据</h3><ul>
<li>每次读取一个字符</li>
</ul>
<pre><code><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">FileInputStream fis  = <span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.txt&quot;</span>);</span><br><span class="line"><span class="keyword">int</span> ch = fis.read();<span class="comment">//当指针指到文件末尾时,ch == -1;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">BufferedInputStream bis = <span class="keyword">new</span> BufferedInputStream(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.txt&quot;</span>));</span><br><span class="line"><span class="keyword">int</span> ch = bis.read();</span><br></pre></td></tr></table></figure>
</code></pre><ul>
<li><p>每次读取多个字符</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">FileInputStream fis  = <span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.txt&quot;</span>);</span><br><span class="line"><span class="keyword">byte</span>[]  bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line"><span class="keyword">int</span> len = fis.read(bytes);<span class="comment">//返回值为真实的读取字节数组的长度</span></span><br></pre></td></tr></table></figure>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">BufferedInputStream bis = <span class="keyword">new</span> BufferedInputStream(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.txt&quot;</span>));</span><br><span class="line"><span class="keyword">byte</span>[]  bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line"><span class="keyword">int</span> len = bis.read(bytes) </span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="拷贝文件"><a href="#拷贝文件" class="headerlink" title="拷贝文件"></a>拷贝文件</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Timer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> <span class="keyword">throws</span> IOException</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        time(<span class="string">&quot;一次读一个字节&quot;</span>,() -&gt; &#123;</span><br><span class="line">            FileInputStream fis = <span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.mp4&quot;</span>);</span><br><span class="line">            FileOutputStream fos = <span class="keyword">new</span> FileOutputStream(<span class="string">&quot;copy1-test.mp4&quot;</span>);</span><br><span class="line">            <span class="keyword">int</span> ch;</span><br><span class="line">            <span class="keyword">while</span> ((ch = fis.read()) != -<span class="number">1</span>) &#123;</span><br><span class="line">                fos.write(ch);</span><br><span class="line">            &#125;</span><br><span class="line">            fis.close();</span><br><span class="line">            fos.close();</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        time(<span class="string">&quot;一次多个字节&quot;</span>,() -&gt;&#123;</span><br><span class="line">            FileInputStream fis = <span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.mp4&quot;</span>);</span><br><span class="line">            FileOutputStream fos = <span class="keyword">new</span> FileOutputStream(<span class="string">&quot;copy2-test.mp4&quot;</span>);</span><br><span class="line">            <span class="keyword">byte</span>[] bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">            <span class="keyword">int</span> ch ;</span><br><span class="line">            <span class="keyword">while</span> ((ch = fis.read(bytes)) != -<span class="number">1</span>) &#123;</span><br><span class="line">                fos.write(bytes);</span><br><span class="line">            &#125;</span><br><span class="line">            fis.close();</span><br><span class="line">            fos.close();</span><br><span class="line">        &#125;);</span><br><span class="line">        time(<span class="string">&quot;字符缓冲流一次一个字节&quot;</span>,()-&gt;&#123;</span><br><span class="line">            BufferedInputStream bis = <span class="keyword">new</span> BufferedInputStream( <span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.mp4&quot;</span>));</span><br><span class="line">            BufferedOutputStream bos = <span class="keyword">new</span> BufferedOutputStream(<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;copy3-test.mp4&quot;</span>));</span><br><span class="line">            <span class="keyword">int</span> ch;</span><br><span class="line">            <span class="keyword">while</span>((ch = bis.read()) != -<span class="number">1</span>)&#123;</span><br><span class="line">                bos.write(ch);</span><br><span class="line">            &#125;</span><br><span class="line">            bis.close();</span><br><span class="line">            bos.close();</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        time(<span class="string">&quot;字符缓冲流一次读多个字节&quot;</span>,()-&gt;&#123;</span><br><span class="line">            BufferedInputStream bis = <span class="keyword">new</span> BufferedInputStream( <span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.mp4&quot;</span>));</span><br><span class="line">            BufferedOutputStream bos = <span class="keyword">new</span> BufferedOutputStream(<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;copy4-test.mp4&quot;</span>));</span><br><span class="line">            <span class="keyword">int</span> len;</span><br><span class="line">            <span class="keyword">byte</span>[] bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line"></span><br><span class="line">            <span class="keyword">while</span>((len = bis.read(bytes)) != -<span class="number">1</span>)&#123;</span><br><span class="line">                bos.write(bytes,<span class="number">0</span>,len);</span><br><span class="line">            &#125;</span><br><span class="line">            bis.close();</span><br><span class="line">            bos.close();</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">time</span><span class="params">(String name,Timer timer)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> start = System.currentTimeMillis();</span><br><span class="line">        timer.run();</span><br><span class="line">        <span class="keyword">long</span> end = System.currentTimeMillis();</span><br><span class="line">        System.out.println(<span class="string">&quot;%s 耗时:%d ms&quot;</span>.formatted(name ,end - start));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">输出：</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">一次读一个字节 耗时:20573 ms</span></span><br><span class="line"><span class="comment">一次多个字节 耗时:16 ms</span></span><br><span class="line"><span class="comment">字符缓冲流一次一个字节 耗时:156 ms</span></span><br><span class="line"><span class="comment">字符缓冲流一次读多个字节 耗时:15 ms</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h2 id="字符流"><a href="#字符流" class="headerlink" title="字符流"></a>字符流</h2><h3 id="字符流读数据"><a href="#字符流读数据" class="headerlink" title="字符流读数据"></a>字符流读数据</h3><ul>
<li><p>一次读一个字符</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">InputStreamReader isr = <span class="keyword">new</span> InputStreamReader(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.txt&quot;</span>),<span class="string">&quot;UTF-8&quot;</span>);<span class="comment">//未指定编码默认为平台编码</span></span><br><span class="line"><span class="keyword">int</span> ch = isr.read();</span><br><span class="line">System.out.print((<span class="keyword">char</span>)ch);</span><br></pre></td></tr></table></figure>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">BufferedReader br = <span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> FileReader(<span class="string">&quot;test.txt&quot;</span>));</span><br><span class="line"><span class="keyword">int</span> ch = br.read();</span><br></pre></td></tr></table></figure>
</li>
<li><p>一次读多个字符</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">InputStreamReader isr = <span class="keyword">new</span> InputStreamReader(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;test.txt&quot;</span>),<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line"><span class="keyword">char</span>[] chars = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">1024</span>];</span><br><span class="line"><span class="keyword">int</span> len = isr.read(chars);</span><br><span class="line">System.out.println(<span class="keyword">new</span> String(chars,<span class="number">0</span>,len));</span><br><span class="line"></span><br></pre></td></tr></table></figure>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">BufferedReader br = <span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> FileReader(<span class="string">&quot;test.txt&quot;</span>));</span><br><span class="line"><span class="keyword">char</span>[] chars = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">1024</span>];</span><br><span class="line"><span class="keyword">int</span> len = br.read(chars);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="字符流写数据"><a href="#字符流写数据" class="headerlink" title="字符流写数据"></a>字符流写数据</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">write</span><span class="params">(<span class="keyword">int</span> c)</span></span>;<span class="comment">//写一个字节</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">write</span><span class="params">(<span class="keyword">char</span> cbuf[])</span></span>;<span class="comment">//写一个字符数组</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">write</span><span class="params">(<span class="keyword">char</span> cbuf[],<span class="keyword">int</span> off,<span class="keyword">int</span> len)</span></span>;<span class="comment">//写一个字符数组[oof,oof+len);</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">write</span><span class="params">(String s)</span></span>;<span class="comment">//写一个字符串</span></span><br></pre></td></tr></table></figure>
<h3 id="拷贝文本"><a href="#拷贝文本" class="headerlink" title="拷贝文本"></a>拷贝文本</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line"></span><br><span class="line">        time(<span class="string">&quot;一次写一个字符&quot;</span>,()-&gt;&#123;</span><br><span class="line">            FileWriter fw = <span class="keyword">new</span> FileWriter(<span class="string">&quot;copy1-test.txt&quot;</span>);</span><br><span class="line">            FileReader fr = <span class="keyword">new</span> FileReader(<span class="string">&quot;test.txt&quot;</span>);</span><br><span class="line">            <span class="keyword">int</span> ch;</span><br><span class="line">            <span class="keyword">while</span> ((ch = fr.read()) != -<span class="number">1</span>)&#123;</span><br><span class="line">                fw.write(ch);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        time(<span class="string">&quot;一次写一个字符数组&quot;</span>,()-&gt;&#123;</span><br><span class="line">            FileWriter fw = <span class="keyword">new</span> FileWriter(<span class="string">&quot;copy2-test.txt&quot;</span>);</span><br><span class="line">            FileReader fr = <span class="keyword">new</span> FileReader(<span class="string">&quot;test.txt&quot;</span>);</span><br><span class="line">            <span class="keyword">char</span>[] chars = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">1024</span>];</span><br><span class="line">            <span class="keyword">int</span> len;</span><br><span class="line">            <span class="keyword">while</span> ((len = fr.read(chars)) != -<span class="number">1</span>)&#123;</span><br><span class="line">                fw.write(chars);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        time(<span class="string">&quot;字符缓冲流一次写一个字符&quot;</span>,()-&gt;&#123;</span><br><span class="line">            FileWriter fw = <span class="keyword">new</span> FileWriter(<span class="string">&quot;copy3-test.txt&quot;</span>);</span><br><span class="line">            FileReader fr = <span class="keyword">new</span> FileReader(<span class="string">&quot;test.txt&quot;</span>);</span><br><span class="line">            BufferedReader br = <span class="keyword">new</span> BufferedReader(fr);</span><br><span class="line">            BufferedWriter bw = <span class="keyword">new</span> BufferedWriter(fw);</span><br><span class="line">            <span class="keyword">int</span> ch;</span><br><span class="line">            <span class="keyword">while</span> ((ch = br.read()) != -<span class="number">1</span>)&#123;</span><br><span class="line">                bw.write(ch);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        time(<span class="string">&quot;字符缓冲流一次写一个字符数组&quot;</span>,()-&gt;&#123;</span><br><span class="line">            FileWriter fw = <span class="keyword">new</span> FileWriter(<span class="string">&quot;copy4-test.txt&quot;</span>);</span><br><span class="line">            FileReader fr = <span class="keyword">new</span> FileReader(<span class="string">&quot;test.txt&quot;</span>);</span><br><span class="line">            BufferedReader br = <span class="keyword">new</span> BufferedReader(fr);</span><br><span class="line">            BufferedWriter bw = <span class="keyword">new</span> BufferedWriter(fw);</span><br><span class="line">            <span class="keyword">char</span>[] chars = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">1024</span>];</span><br><span class="line">            <span class="keyword">int</span> len;</span><br><span class="line">            <span class="keyword">while</span> ((len = br.read(chars)) != -<span class="number">1</span>)&#123;</span><br><span class="line">                bw.write(chars);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">time</span><span class="params">(String name,Timer timer)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> start = System.currentTimeMillis();</span><br><span class="line">        timer.run();</span><br><span class="line">        <span class="keyword">long</span> end = System.currentTimeMillis();</span><br><span class="line">        System.out.println(<span class="string">&quot;%s 耗时:%d ms&quot;</span>.formatted(name ,end - start));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="流异常处理"><a href="#流异常处理" class="headerlink" title="流异常处理"></a>流异常处理</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">FileOutputStream fos = <span class="keyword">null</span>;</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    fos = <span class="keyword">new</span> FileOutputStream(<span class="string">&quot;test.txt&quot;</span>,<span class="keyword">true</span>);</span><br><span class="line">    fos.write(<span class="string">&quot;test&quot;</span>.getBytes());</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">catch</span> (FileNotFoundException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (fos != <span class="keyword">null</span>)&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fos.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述方法过于复杂,可以使用如下方法,该方法可以将流自动关闭</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>( FileOutputStream fos  = <span class="keyword">new</span> FileOutputStream(<span class="string">&quot;test.txt&quot;</span>)) &#123;</span><br><span class="line">    fos.write(<span class="string">&quot;test&quot;</span>.getBytes());</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">catch</span> (FileNotFoundException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="存取对象"><a href="#存取对象" class="headerlink" title="存取对象"></a>存取对象</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">    Student()&#123;&#125;</span><br><span class="line">    Student(String name,<span class="keyword">int</span> age)&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>对象序列化</p>
<p>如果一个对象需要被写入文件，可以让这个对象实现<code>Serializable</code>接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ObjectOutputStream oss = <span class="keyword">new</span> ObjectOutputStream(<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;student.txt&quot;</span>));</span><br><span class="line">Student student = <span class="keyword">new</span> Student(<span class="string">&quot;孙悟空&quot;</span>,<span class="number">18</span>);</span><br><span class="line">oss.writeObject(student);</span><br><span class="line">oss.close();</span><br></pre></td></tr></table></figure>
<p>如果一个对象的属性不限被序列化,使用<code>transient</code>关键字</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">transient</span> <span class="keyword">int</span> age;</span><br></pre></td></tr></table></figure>
</li>
<li><p>对象反序列化</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ObjectInputStream ois = <span class="keyword">new</span> ObjectInputStream(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;student.txt&quot;</span>));</span><br><span class="line">Object obj = ois.readObject();</span><br><span class="line">Student student = (Student)obj;</span><br><span class="line">System.out.println(student);</span><br><span class="line"><span class="comment">//Student&#123;name=&#x27;孙悟空&#x27;, age=18&#125;</span></span><br></pre></td></tr></table></figure>
<p>使用<code>transient</code>关键字修饰后</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Student&#123;name=&#x27;孙悟空&#x27;, age=0&#125;</span></span><br></pre></td></tr></table></figure>
<p>当序列化后的的对象被修改时，反序列化会抛出异常<code>java.io.InvalidClassException</code></p>
<p>如果想然被修改后的对象反序列化抛出异常，可以使用<code>serialVersionUID</code>指定序列化<code>id</code>值（建议使用<code>private</code>修饰，必须使用<code>static final</code>修饰）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">42L</span>;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="注解"><a href="#注解" class="headerlink" title="注解"></a>注解</h1><p>注解是<code>java</code>中放在源代码的类，方法，注释，属性等之前的一种特殊的<code>注释</code></p>
<ul>
<li><p>注释不能被编译器读取</p>
</li>
<li><p>注解可以被编译器读取</p>
</li>
</ul>
<h2 id="元注解"><a href="#元注解" class="headerlink" title="元注解"></a>元注解</h2><p>元注解是用于修饰注解的注解</p>
<ul>
<li><code>@Target</code> 说明一个注解的使用范围，如在类、方法、属性等</li>
<li><code>@Retention</code> 说明该注解的生命周期 (SOURCE &lt; CLASS &lt; RUNTIME）默认值<code>CLASS</code>该注解不会被正在运行的程序读取</li>
<li><code>@Documented</code> 说明该注解会被包含在javadoc中</li>
<li><code>@Inherited</code> 说明子类可以继承该注解</li>
</ul>
<h2 id="自定义注解"><a href="#自定义注解" class="headerlink" title="自定义注解"></a>自定义注解</h2><ul>
<li><p>定义注解中的字段时，应该采用:<code>类型名 字段名()</code>的形式定义</p>
</li>
<li><p>可以为字段指定默认值，若不指定默认值，则在使用时必须为字段赋值</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="meta">@Inherited</span></span><br><span class="line"><span class="meta">@Target(value = &#123;ElementType.METHOD,ElementType.FIELD&#125;)</span></span><br><span class="line"><span class="meta">@Retention(value = RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@interface</span> MyAnnotation1 &#123;</span><br><span class="line">    <span class="function">String <span class="title">methodName</span><span class="params">()</span> </span>;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">id</span><span class="params">()</span> <span class="keyword">default</span>  -1</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="meta">@MyAnnotation1(methodName = &quot;print&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//或者</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="meta">@MyAnnotation1(methodName = &quot;print&quot;,id = 1)</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>若自定义的注解只有一个字段，该字段可以用<code>value</code>表示，使用该注解时，可以不用显示指定的字段名称，若不使用<code>value</code>表示该字段，则必须显示指出字段名称</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="meta">@Inherited</span></span><br><span class="line"><span class="meta">@Target(value = &#123;ElementType.METHOD,ElementType.FIELD&#125;)</span></span><br><span class="line"><span class="meta">@Retention(value = RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@interface</span> MyAnnotation2 &#123;</span><br><span class="line">    <span class="function">String <span class="title">value</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//使用</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Test</span></span>&#123;</span><br><span class="line">    <span class="meta">@MyAnnotation2(&quot;print&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="内置注解"><a href="#内置注解" class="headerlink" title="内置注解"></a>内置注解</h2><ul>
<li><code>@Override</code>该注解用于修饰方法，表示被修饰的方法将重写超类的方法</li>
<li><code>@Deprecated</code>该注解用于修饰过时的方法、属性和类，表示不建议程序员使用该方法、属性和类，</li>
<li><code>@SuppressWarnings</code>用于抑制编译警告</li>
</ul>
<h1 id="反射"><a href="#反射" class="headerlink" title="反射"></a>反射</h1><h2 id="类加载"><a href="#类加载" class="headerlink" title="类加载"></a>类加载</h2><p>当程序需要某个类且该类在内存中时，<code>jvm</code>会通过类的加载、类的连接、类的初始化三个步骤对类进行初始化。</p>
<ul>
<li><p>类加载</p>
<ul>
<li>将<code>class</code>文件读入内存，并为之创建对象</li>
</ul>
</li>
<li><p>类连接</p>
<ul>
<li>验证阶段：验证被加载的类是否有正确的内部结构</li>
<li>准备阶段：为类的变量分配内存，设置默认初始值</li>
<li>解析阶段：将类的二进制符号引用替换为直接引用</li>
</ul>
</li>
<li><p>类初始化</p>
<p>初始化步骤：</p>
<ul>
<li>假如类还未被加载和连接，则程序先加载并连接该类</li>
<li>假如该类的直接父类还未被初始化，则先初始化其直接父类（所以每次<code>Object</code>类先被初始化）</li>
<li>假如类中有初始化语句，则系统依次执行这些初始化语句</li>
</ul>
<p>可以初始化类的时机：</p>
<ul>
<li>创建类的实例</li>
<li>调用类的类方法</li>
<li>访问类或者接口的类变量，或者为该类变量赋值</li>
<li>使用反射方式来强制创建某个类或接口对应的<code>java.lang.Class</code>对象</li>
<li>初始化某个类的子类</li>
<li>直接使用java.exe命令来运行某个主类</li>
</ul>
</li>
</ul>
<h2 id="类加载器"><a href="#类加载器" class="headerlink" title="类加载器"></a>类加载器</h2><h3 id="JVM的类加载机制"><a href="#JVM的类加载机制" class="headerlink" title="JVM的类加载机制"></a>JVM的类加载机制</h3><ul>
<li>全盘负责：就是当一个类加载器负责加载某个<code>Class</code>时，该Class所依赖的和引用的其他<code>Class</code>也将由该类加载器负责载入，除<br>非显示使用另外一个类加载器来载入</li>
<li>父类委托：就是当一个类加载器负责加载某个<code>Class</code>时，先让父类加载器试图加载该<code>Class</code>，只有在父类加载器无法加载该类<br>时才尝试从自己的类路径中加载该类</li>
<li>缓存机制：保证所有加载过的<code>Class</code>都会被缓存，当程序需要使用某个<code>Class</code>对象时，类加载器先从缓存区中搜索该<code>Class</code>，只<br>有当缓存区中不存在该<code>Class</code>对象时，系统才会读取该类对应的二进制数据，并将其转换成<code>Class</code>对象，存储到缓存区<br>日</li>
</ul>
<h3 id="类加载器-1"><a href="#类加载器-1" class="headerlink" title="类加载器"></a>类加载器</h3><p>Java运行时具有以下内置类加载器：</p>
<ul>
<li><p><code>Bootstrap class loader</code>。它是虚拟机的内置类加载器，通常表示为<code>null</code>，并且没有父<code>null</code>。</p>
</li>
<li><p><code>Platform class loader</code>。平台类加载器可以看到所有平台类 ，可以将其用作<code>ClassLoader</code>实例的父<code>ClassLoader</code>。平台类包括由平台类加载器或其祖先定义的Java SE平台API，其实现类和JDK特定的运行时类。</p>
</li>
<li><p><code>System class loader</code>。它也被称为应用程序类加载器，与平台类加载器不同。系统类加载器通常用于定义应用程序类路径，模块路径和JDK特定工具上的类。</p>
<p><code>Bootstrap是Platform的父类，Platform是System的父类</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">ClassLoader c1 = ClassLoader.getSystemClassLoader();</span><br><span class="line">System.out.println(c1);</span><br><span class="line"></span><br><span class="line">ClassLoader c2 = c1.getParent();</span><br><span class="line">System.out.println(c2);</span><br><span class="line"></span><br><span class="line">ClassLoader c3 = c2.getParent();</span><br><span class="line">System.out.println(c3);</span><br><span class="line"></span><br><span class="line"><span class="comment">//$AppClassLoader</span></span><br><span class="line"><span class="comment">//$PlatformClassLoader</span></span><br><span class="line"><span class="comment">//null</span></span><br><span class="line"><span class="comment">//null</span></span><br><span class="line"><span class="comment">//$AppClassLoader</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="类初始化"><a href="#类初始化" class="headerlink" title="类初始化"></a>类初始化</h2><ul>
<li>类的主动引用（一定会发生类的初始化）<ul>
<li>当虚拟机启动，先初始化main方法所在的类</li>
<li>new一个类的对象</li>
<li>调用类的静态成员（除了final常量）和静态方法</li>
<li>使用java.lang.reflect包的方法对类进行反射调用</li>
<li>当初始化一个类，如果其父类没有被初始化，则先会初始化它的父类</li>
</ul>
</li>
<li>类的被动引用（不会发生类的初始化）<ul>
<li>当访问一个静态域时，只有真正声明这个域的类才会被初始化。如：当通过子类引用父类的静态变量不会导致子类初始化</li>
<li>通过数组定义类引用，不会触发此类的初始化</li>
<li>引用常量不会触发此类的初始化（常量在链接阶段就存入调用类的常量池中了）</li>
</ul>
</li>
</ul>
<h2 id="反射获取类的信息"><a href="#反射获取类的信息" class="headerlink" title="反射获取类的信息"></a>反射获取类的信息</h2><h3 id="获取Class类的对象"><a href="#获取Class类的对象" class="headerlink" title="获取Class类的对象"></a>获取<code>Class</code>类的对象</h3><ul>
<li><p>一个类在内存中只有一个<code>Class</code>对象</p>
</li>
<li><p>一个类被加载后整个结构都被封装在内存中</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException </span>&#123;</span><br><span class="line">        <span class="comment">//通过类获得</span></span><br><span class="line">        Class&lt;Student&gt;  c1 = Student.class;</span><br><span class="line">        System.out.println(c1);</span><br><span class="line"></span><br><span class="line">       </span><br><span class="line">        Class&lt;Student&gt; c2 = Student.class;</span><br><span class="line">        System.out.println(c1 == c2);</span><br><span class="line"></span><br><span class="line">         <span class="comment">//通过类的实例对象获得</span></span><br><span class="line">        Student c3 = <span class="keyword">new</span> Student();</span><br><span class="line">        System.out.println(c3.getClass() == c1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//通过反射获取类的对象</span></span><br><span class="line">        Class&lt;?&gt; c4 = Class.forName(<span class="string">&quot;test.Student&quot;</span>);</span><br><span class="line">        System.out.println(c4 == c1);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//class test.Student</span></span><br><span class="line"><span class="comment">//true</span></span><br><span class="line"><span class="comment">//true</span></span><br><span class="line"><span class="comment">//true</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Class c1 = Object.class;<span class="comment">//class java.lang.Object</span></span><br><span class="line">Class c2 = Comparable.class;<span class="comment">//interface java.lang.Comparable</span></span><br><span class="line">Class c3 = String.class;<span class="comment">//class java.lang.String</span></span><br><span class="line">Class c4 = String[].class;<span class="comment">//class [Ljava.lang.String;</span></span><br><span class="line">Class c5 = <span class="keyword">int</span>.class;<span class="comment">//int</span></span><br><span class="line">Class c6 = <span class="keyword">int</span>[][].class;<span class="comment">//class [[I</span></span><br><span class="line">Class c7 = Integer.class;<span class="comment">//class java.lang.Integer</span></span><br><span class="line">Class c8 = Override.class;<span class="comment">//interface java.lang.Override</span></span><br><span class="line">Class c9 = ElementType.class;<span class="comment">//class java.lang.annotation.ElementType</span></span><br><span class="line">Class c10 = <span class="keyword">void</span>.class;<span class="comment">//void</span></span><br><span class="line">Class c11 =  Class.class;<span class="comment">//class java.lang.Class</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="获取类的成员"><a href="#获取类的成员" class="headerlink" title="获取类的成员"></a>获取类的成员</h3><div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Field[] getFields()</code></td>
<td>获取该类所有共有属性</td>
</tr>
<tr>
<td><code>Field[] getDeclaredFields()</code></td>
<td>获取该类所有的属性</td>
</tr>
<tr>
<td><code>Field getField()</code></td>
<td>获取该类指定的共有属性</td>
</tr>
<tr>
<td><code>Field getDeclaredField()</code></td>
<td>获取该类指定的属性</td>
</tr>
<tr>
<td><code>Method[] getMethods()</code></td>
<td>获取该类的所有共有方法，包括从父类继承来的方法</td>
</tr>
<tr>
<td><code>Method[] getDeclaredMethods()</code></td>
<td>获取该类的所有方法</td>
</tr>
<tr>
<td><code>Method getMethod()</code></td>
<td>获取该类指定的共有方法</td>
</tr>
<tr>
<td><code>Method getDeclaredMethod()</code></td>
<td>获取该类指定的方法</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException </span>&#123;</span><br><span class="line">        <span class="comment">//通过反射获取类的对象</span></span><br><span class="line">        Class&lt;?&gt; aClass = Class.forName(<span class="string">&quot;test.Student&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取所有共有属性</span></span><br><span class="line">        <span class="keyword">for</span> (Field field : aClass.getFields()) &#123;</span><br><span class="line">            System.out.println(field);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;=======================&quot;</span>);</span><br><span class="line">        <span class="comment">//获取所有属性</span></span><br><span class="line">        <span class="keyword">for</span> (Field declaredField : aClass.getDeclaredFields()) &#123;</span><br><span class="line">            System.out.println(declaredField);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;=======================&quot;</span>);</span><br><span class="line">        <span class="comment">//获取指定共有属性</span></span><br><span class="line">        System.out.println(aClass.getField(<span class="string">&quot;id&quot;</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;=======================&quot;</span>);</span><br><span class="line">        <span class="comment">//获取指定属性</span></span><br><span class="line">        System.out.println(aClass.getDeclaredField(<span class="string">&quot;name&quot;</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;=======================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取共有方法，包括父类的方法</span></span><br><span class="line">        <span class="keyword">for</span> (Method method : aClass.getMethods()) &#123;</span><br><span class="line">            System.out.println(method);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;=======================&quot;</span>);</span><br><span class="line">        <span class="comment">//获取所有方法，不包括父类</span></span><br><span class="line">        <span class="keyword">for</span> (Method declaredMethod : aClass.getDeclaredMethods()) &#123;</span><br><span class="line">            System.out.println(declaredMethod);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//获取指定共有方法</span></span><br><span class="line">        System.out.println(aClass.getMethod(<span class="string">&quot;getName&quot;</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;=======================&quot;</span>);</span><br><span class="line">        <span class="comment">//获取指定方法</span></span><br><span class="line">        System.out.println(aClass.getDeclaredMethod(<span class="string">&quot;getId&quot;</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;=======================&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.id</span></span><br><span class="line"><span class="comment">=======================</span></span><br><span class="line"><span class="comment">private java.lang.String test.Student.name</span></span><br><span class="line"><span class="comment">private int test.Student.age</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.id</span></span><br><span class="line"><span class="comment">=======================</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.id</span></span><br><span class="line"><span class="comment">=======================</span></span><br><span class="line"><span class="comment">private java.lang.String test.Student.name</span></span><br><span class="line"><span class="comment">=======================</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.getName()</span></span><br><span class="line"><span class="comment">public void test.Student.setName(java.lang.String)</span></span><br><span class="line"><span class="comment">public void test.Student.setAge(int)</span></span><br><span class="line"><span class="comment">public int test.Student.getAge()</span></span><br><span class="line"><span class="comment">public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException</span></span><br><span class="line"><span class="comment">public final void java.lang.Object.wait() throws java.lang.InterruptedException</span></span><br><span class="line"><span class="comment">public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException</span></span><br><span class="line"><span class="comment">public boolean java.lang.Object.equals(java.lang.Object)</span></span><br><span class="line"><span class="comment">public java.lang.String java.lang.Object.toString()</span></span><br><span class="line"><span class="comment">public native int java.lang.Object.hashCode()</span></span><br><span class="line"><span class="comment">public final native java.lang.Class java.lang.Object.getClass()</span></span><br><span class="line"><span class="comment">public final native void java.lang.Object.notify()</span></span><br><span class="line"><span class="comment">public final native void java.lang.Object.notifyAll()</span></span><br><span class="line"><span class="comment">=======================</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.getName()</span></span><br><span class="line"><span class="comment">public void test.Student.setName(java.lang.String)</span></span><br><span class="line"><span class="comment">private java.lang.String test.Student.getId()</span></span><br><span class="line"><span class="comment">public void test.Student.setAge(int)</span></span><br><span class="line"><span class="comment">public int test.Student.getAge()</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.getName()</span></span><br><span class="line"><span class="comment">=======================</span></span><br><span class="line"><span class="comment">private java.lang.String test.Student.getId()</span></span><br><span class="line"><span class="comment">=======================</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<h3 id="获取类的构造器"><a href="#获取类的构造器" class="headerlink" title="获取类的构造器"></a>获取类的构造器</h3><div class="table-container">
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Constructor getConstructor()</code></td>
<td>获取指定共有构造器（通过参数类型确认构造器）,私有构造器无法获取</td>
</tr>
<tr>
<td><code>Constructor getDeclaredConstructor()</code></td>
<td>获取指定构造器（通过参数类型确认构造器）</td>
</tr>
<tr>
<td><code>Constructor[] getConstructors()</code></td>
<td>获取所有共有构造器</td>
</tr>
<tr>
<td><code>Constructor[] getDeclaredConstructor()</code></td>
<td>获取所有构造器</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//通过反射获取类的对象</span></span><br><span class="line">Class&lt;?&gt; aClass = Class.forName(<span class="string">&quot;test.Student&quot;</span>);</span><br><span class="line"><span class="comment">//获取指定共有构造器</span></span><br><span class="line">Constructor&lt;?&gt; constructor1 = aClass.getConstructor();</span><br><span class="line">System.out.println(constructor1);</span><br><span class="line">System.out.println(<span class="string">&quot;=====================&quot;</span>);</span><br><span class="line"><span class="comment">//获取指定构造器</span></span><br><span class="line">Constructor&lt;?&gt; constructor2 = aClass.getDeclaredConstructor(String.class,<span class="keyword">int</span>.class,String.class);</span><br><span class="line">System.out.println(constructor2);</span><br><span class="line">System.out.println(<span class="string">&quot;=====================&quot;</span>);</span><br><span class="line"><span class="keyword">for</span> (Constructor&lt;?&gt; constructor : aClass.getConstructors()) &#123;</span><br><span class="line">    System.out.println(constructor);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println(<span class="string">&quot;=====================&quot;</span>);</span><br><span class="line"><span class="keyword">for</span> (Constructor&lt;?&gt; constructor : aClass.getDeclaredConstructors()) &#123;</span><br><span class="line">    System.out.println(constructor);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">public test.Student()</span></span><br><span class="line"><span class="comment">=====================</span></span><br><span class="line"><span class="comment">private test.Student(java.lang.String,int,java.lang.String)</span></span><br><span class="line"><span class="comment">=====================</span></span><br><span class="line"><span class="comment">public test.Student()</span></span><br><span class="line"><span class="comment">=====================</span></span><br><span class="line"><span class="comment">public test.Student()</span></span><br><span class="line"><span class="comment">private test.Student(java.lang.String,int,java.lang.String)</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>通过构造器创建对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//通过反射获取类的对象</span></span><br><span class="line">Class&lt;?&gt; aClass = Class.forName(<span class="string">&quot;test.Student&quot;</span>);</span><br><span class="line"><span class="comment">//使用无参构造器创建对象</span></span><br><span class="line">Student s1 = (Student)aClass.newInstance();</span><br><span class="line">System.out.println(s1);</span><br><span class="line">Constructor&lt;?&gt; c1 = aClass.getConstructor();</span><br><span class="line">Student s2 = (Student)c1.newInstance();</span><br><span class="line">System.out.println(s2);</span><br><span class="line"><span class="comment">//使用有参构造器创建对象</span></span><br><span class="line"><span class="comment">//        Constructor&lt;?&gt; c2 = aClass.getConstructor(String.class,int.class,String.class);//当构造器为私有时无法使用</span></span><br><span class="line"><span class="comment">//        Student s3 = (Student) c2.newInstance(&quot;孙悟空&quot;, 18, &quot;110&quot;);</span></span><br><span class="line"><span class="comment">//        System.out.println(s3);</span></span><br><span class="line"></span><br><span class="line">Constructor&lt;?&gt; c3 = aClass.getDeclaredConstructor(String.class,<span class="keyword">int</span>.class,String.class);</span><br><span class="line">c3.setAccessible(<span class="keyword">true</span>);<span class="comment">//设置安全模式为不检查，若不设置，构造器为私有时无法使用</span></span><br><span class="line">Student s4 = (Student) c3.newInstance(<span class="string">&quot;孙悟空&quot;</span>, <span class="number">18</span>, <span class="string">&quot;110&quot;</span>);</span><br><span class="line">System.out.println(s4);</span><br></pre></td></tr></table></figure>
<h3 id="操作对象的成员"><a href="#操作对象的成员" class="headerlink" title="操作对象的成员"></a>操作对象的成员</h3><p>当对象的成员为私有时，直接创建的对象无法直接操作私有成员，但可以使用反射创建的对象操作该成员，需要为该成员设置<code>setAccessible(true)</code>后就可以字节操作，该方法可以启用和禁用安全检测（true表示关闭，false表示开启），关闭安全检测后，使用反射会更快</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">Class&lt;?&gt; aClass = Class.forName(<span class="string">&quot;test.Student&quot;</span>);</span><br><span class="line"><span class="comment">//使用无参构造器创建对象</span></span><br><span class="line"></span><br><span class="line">Student s1 = (Student)aClass.newInstance();</span><br><span class="line"></span><br><span class="line"><span class="comment">//一般方法操共有方法和属性</span></span><br><span class="line">s1.setName(<span class="string">&quot;孙悟空&quot;</span>);</span><br><span class="line">s1.setAge(<span class="number">18</span>);</span><br><span class="line">s1.id = <span class="string">&quot;110&quot;</span>;</span><br><span class="line">System.out.println(s1);</span><br><span class="line"></span><br><span class="line"><span class="comment">//反射操作所有方法和属性</span></span><br><span class="line"><span class="comment">//使用共有方法</span></span><br><span class="line">Method setAge = aClass.getDeclaredMethod(<span class="string">&quot;setAge&quot;</span>, <span class="keyword">int</span>.class);</span><br><span class="line">setAge.invoke(s1,<span class="number">18</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置私有属性值</span></span><br><span class="line">Field name = aClass.getDeclaredField(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">name.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">name.set(s1,<span class="string">&quot;孙悟空&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用私有方法</span></span><br><span class="line">Method setName = aClass.getDeclaredMethod(<span class="string">&quot;setId&quot;</span>, String.class);</span><br><span class="line">setName.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">setName.invoke(s1,<span class="string">&quot;110&quot;</span>);</span><br><span class="line"></span><br><span class="line">System.out.println(s1);</span><br></pre></td></tr></table></figure>
<h3 id="反射与泛型"><a href="#反射与泛型" class="headerlink" title="反射与泛型"></a>反射与泛型</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">(Map&lt;String,Student&gt; map, List&lt;Student&gt; list)</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test1&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String, Student&gt; <span class="title">test2</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test02&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException </span>&#123;</span><br><span class="line">        Class&lt;?&gt; aClass = Class.forName(<span class="string">&quot;test.Main&quot;</span>);</span><br><span class="line">        Method test1 = aClass.getMethod(<span class="string">&quot;test1&quot;</span>, Map.class, List.class);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (Type genericParameterType : test1.getGenericParameterTypes()) &#123;</span><br><span class="line">            System.out.println(genericParameterType);</span><br><span class="line">            <span class="keyword">if</span> (genericParameterType <span class="keyword">instanceof</span> ParameterizedType)&#123;</span><br><span class="line">                <span class="keyword">for</span> (Type actualTypeArgument : ((ParameterizedType) genericParameterType).getActualTypeArguments()) &#123;</span><br><span class="line">                    System.out.println(actualTypeArgument);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;======&quot;</span>);</span><br><span class="line">        Method test2 = aClass.getMethod(<span class="string">&quot;test2&quot;</span>);</span><br><span class="line">        Type genericReturnType = test2.getGenericReturnType();</span><br><span class="line">        <span class="keyword">if</span> (genericReturnType <span class="keyword">instanceof</span> ParameterizedType)&#123;</span><br><span class="line">            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();</span><br><span class="line">            <span class="keyword">for</span> (Type type:actualTypeArguments)&#123;</span><br><span class="line">                System.out.println(type);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="反射与注解"><a href="#反射与注解" class="headerlink" title="反射与注解"></a>反射与注解</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ClassNotFoundException</span>&#123;</span><br><span class="line">    Class&lt;?&gt; aClass = Class.forName(<span class="string">&quot;test.Student&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span> (Field declaredField : aClass.getDeclaredFields()) &#123;</span><br><span class="line">        <span class="keyword">if</span> (declaredField.getDeclaredAnnotation(AnnotationField.class) <span class="keyword">instanceof</span> AnnotationField)&#123;</span><br><span class="line">            declaredField.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">            AnnotationField declaredAnnotation = declaredField.getDeclaredAnnotation(AnnotationField.class);</span><br><span class="line">            System.out.println(<span class="string">&quot;%s %s %d&quot;</span>.formatted(declaredAnnotation.columnName() , declaredAnnotation.type(),declaredAnnotation.length()));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    System.out.println(<span class="string">&quot;===================&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span> (Method method : aClass.getDeclaredMethods()) &#123;</span><br><span class="line">        <span class="keyword">if</span> (method.getDeclaredAnnotation(AnnotationMethod.class) <span class="keyword">instanceof</span> AnnotationMethod)&#123;</span><br><span class="line">            method.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">            AnnotationMethod declaredAnnotation = method.getDeclaredAnnotation(AnnotationMethod.class);</span><br><span class="line">            System.out.println(declaredAnnotation);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">age int 2</span></span><br><span class="line"><span class="comment">id varchar 10</span></span><br><span class="line"><span class="comment">===================</span></span><br><span class="line"><span class="comment"><span class="doctag">@test</span>.AnnotationMethod(&quot;method&quot;)</span></span><br><span class="line"><span class="comment"><span class="doctag">@test</span>.AnnotationMethod(&quot;method&quot;)</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
<p>使用<code>org.reflections.Reflections</code>库批量扫描注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">Set&lt;Field&gt; fieldsAnnotatedWith = <span class="keyword">new</span> Reflections(<span class="string">&quot;test&quot;</span>, Scanners.FieldsAnnotated)</span><br><span class="line">    .getFieldsAnnotatedWith(AnnotationField.class);</span><br><span class="line"><span class="keyword">for</span> (Field field:fieldsAnnotatedWith)&#123;</span><br><span class="line">    System.out.println(field);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println(<span class="string">&quot;================&quot;</span>);</span><br><span class="line">Set&lt;Method&gt; methodsAnnotatedWith = <span class="keyword">new</span> Reflections(<span class="string">&quot;test&quot;</span>,Scanners.MethodsAnnotated)</span><br><span class="line">    .getMethodsAnnotatedWith(AnnotationMethod.class);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (Method method:methodsAnnotatedWith)&#123;</span><br><span class="line">    System.out.println(method);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println(<span class="string">&quot;================&quot;</span>);</span><br><span class="line"></span><br><span class="line">Set&lt;Class&lt;?&gt;&gt; typesAnnotatedWith = <span class="keyword">new</span> Reflections(<span class="string">&quot;test&quot;</span>,Scanners.TypesAnnotated)</span><br><span class="line">    .getTypesAnnotatedWith(AnnotationTable.class);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (Class&lt;?&gt; c: typesAnnotatedWith)&#123;</span><br><span class="line">    System.out.println(c);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println(<span class="string">&quot;================&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">91 [main] INFO org.reflections.Reflections - Reflections took 74 ms to scan 1 urls, producing 1 keys and 3 values</span></span><br><span class="line"><span class="comment">private java.lang.String test.Student.name</span></span><br><span class="line"><span class="comment">private int test.Student.age</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.id</span></span><br><span class="line"><span class="comment">================</span></span><br><span class="line"><span class="comment">113 [main] INFO org.reflections.Reflections - Reflections took 11 ms to scan 1 urls, producing 1 keys and 6 values</span></span><br><span class="line"><span class="comment">123 [main] INFO org.reflections.Reflections - Reflections took 6 ms to scan 1 urls, producing 3 keys and 6 values</span></span><br><span class="line"><span class="comment">public int test.Student.getAge()</span></span><br><span class="line"><span class="comment">private java.lang.String test.Student.getId()</span></span><br><span class="line"><span class="comment">public void test.Student.setName(java.lang.String)</span></span><br><span class="line"><span class="comment">public java.lang.String test.Student.getName()</span></span><br><span class="line"><span class="comment">public void test.Student.setAge(int)</span></span><br><span class="line"><span class="comment">public void test.Student.setId(java.lang.String)</span></span><br><span class="line"><span class="comment">================</span></span><br><span class="line"><span class="comment">class test.Student</span></span><br><span class="line"><span class="comment">class test.Teacher</span></span><br><span class="line"><span class="comment">================</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用该库需要准确告知扫描器类型，否则无法扫描</p>
</blockquote>
<h1 id="网络"><a href="#网络" class="headerlink" title="网络"></a>网络</h1><h2 id="InetAddress类"><a href="#InetAddress类" class="headerlink" title="InetAddress类"></a>InetAddress类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">InetAddress byName = InetAddress.getByName(<span class="string">&quot;******&quot;</span>);</span><br><span class="line">System.out.println(<span class="string">&quot;主机名:&quot;</span>+byName.getHostName());</span><br><span class="line">System.out.println(<span class="string">&quot;ip地址:&quot;</span>+byName.getHostAddress());</span><br><span class="line">System.out.println(byName);</span><br></pre></td></tr></table></figure>
<h2 id="UDP通信练习"><a href="#UDP通信练习" class="headerlink" title="UDP通信练习"></a>UDP通信练习</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UDPSend</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        InetAddress inetAddress = InetAddress.getByName(<span class="string">&quot;hostIp&quot;</span>);</span><br><span class="line"></span><br><span class="line">        DatagramSocket ds = <span class="keyword">new</span> DatagramSocket();</span><br><span class="line">        BufferedReader br = <span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> InputStreamReader(System.in));</span><br><span class="line">        String line;</span><br><span class="line">        <span class="keyword">while</span> ((line = br.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">            DatagramPacket datagramPacket = <span class="keyword">new</span> DatagramPacket(line.getBytes(),line.getBytes().length,inetAddress,<span class="number">8888</span>);<span class="comment">//指定数据报发送的地址和端口</span></span><br><span class="line"></span><br><span class="line">            ds.send(datagramPacket);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//接受数据并打印</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UDPReceive</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        DatagramSocket socket = <span class="keyword">new</span> DatagramSocket(<span class="number">8888</span>);</span><br><span class="line">        <span class="keyword">byte</span>[] bytes = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">            DatagramPacket dp = <span class="keyword">new</span> DatagramPacket(bytes,bytes.length );</span><br><span class="line">            socket.receive(dp);</span><br><span class="line">            System.out.println(<span class="string">&quot;接受到:&quot;</span>+<span class="keyword">new</span> String(dp.getData(),<span class="number">0</span>,dp.getLength()));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="TCP通信练习"><a href="#TCP通信练习" class="headerlink" title="TCP通信练习"></a>TCP通信练习</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TCPReceive</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        ServerSocket socket = <span class="keyword">new</span> ServerSocket(<span class="number">8888</span>);</span><br><span class="line">        Socket s = socket.accept();</span><br><span class="line">        InputStreamReader reader = <span class="keyword">new</span> InputStreamReader(s.getInputStream());</span><br><span class="line">        <span class="keyword">char</span>[] bytes = <span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="keyword">int</span> len;</span><br><span class="line">        <span class="keyword">while</span> ( (len = reader.read(bytes)) != -<span class="number">1</span>)&#123;</span><br><span class="line"></span><br><span class="line">            System.out.println(<span class="keyword">new</span> String(bytes,<span class="number">0</span>,len));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TCPSend</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        Socket socket = <span class="keyword">new</span> Socket(<span class="string">&quot;127.0.0.1&quot;</span>,<span class="number">8888</span>);</span><br><span class="line">        BufferedReader bufferedReader = <span class="keyword">new</span> BufferedReader(<span class="keyword">new</span> InputStreamReader(System.in));</span><br><span class="line"></span><br><span class="line">        OutputStream outputStream = socket.getOutputStream();</span><br><span class="line">        String line = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">while</span> ((line = bufferedReader.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">            outputStream.write(line.getBytes());</span><br><span class="line">            outputStream.flush();</span><br><span class="line">        &#125;</span><br><span class="line">        outputStream.close();</span><br><span class="line">        socket.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="并发"><a href="#并发" class="headerlink" title="并发"></a>并发</h1><h2 id="多线程"><a href="#多线程" class="headerlink" title="多线程"></a>多线程</h2><h2 id="多进程"><a href="#多进程" class="headerlink" title="多进程"></a>多进程</h2><p><img src="http://124.221.123.212:8080/images/image-20211119173309942.png" alt="image-20211119173309942"></p>
<p><img src="http://124.221.123.212:8080/images/image-20211119173620057.png" alt="image-20211119173620057"></p>
<h2 id="常用类线程安全对比"><a href="#常用类线程安全对比" class="headerlink" title="常用类线程安全对比"></a>常用类线程安全对比</h2><div class="table-container">
<table>
<thead>
<tr>
<th>JDK版本</th>
<th>不执行同步，更快</th>
<th>执行同步，安全性高</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>JDK5</code></td>
<td><code>StringBuilder</code></td>
<td><code>StringBuffer</code></td>
</tr>
<tr>
<td><code>JDK2</code></td>
<td><code>ArrayList</code></td>
<td><code>Vector</code></td>
</tr>
<tr>
<td><code>JDK2</code></td>
<td><code>HashMap</code></td>
<td><code>HashTable</code></td>
</tr>
</tbody>
</table>
</div>
<h1 id="问题汇总"><a href="#问题汇总" class="headerlink" title="问题汇总"></a>问题汇总</h1><h2 id="待解决问题"><a href="#待解决问题" class="headerlink" title="待解决问题"></a>待解决问题</h2><ul>
<li>List.of()的重载有很多，有1~10个参数的重载，但明明有可变参数的重载为什么还要写那么多的重载</li>
</ul>
<p>包名不能是jdk中的含有的包的名称</p>
<ul>
<li>为什么<code>Reflections</code>库可以扫描到未被<code>@Retention(RetentionPolicy.RUNTIME)</code>修饰的注解</li>
</ul>
<h2 id="已解决问题"><a href="#已解决问题" class="headerlink" title="已解决问题"></a>已解决问题</h2><ul>
<li>Error: A JNI error has occurred, please check your installation and try again</li>
</ul>
<p><img src="http://124.221.123.212:8080/images/屏幕截图 2021-11-19 200235.png" alt="屏幕截图 2021-11-19 200235"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">dreamcatcherdeng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/JAVA/">https://dreamcatcherdeng.github.io/personal-blog/2022/07/13/JAVA/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-72rd8e.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/personal-blog/2022/07/13/MySQL/"><img class="prev-cover" src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-e7ek7k.jpg" onerror="onerror=null;src='/personal-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">MySQL</div></div></a></div><div class="next-post pull-right"><a href="/personal-blog/2022/07/13/ViewPager/"><img class="next-cover" src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-0qxx54.jpg" onerror="onerror=null;src='/personal-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">ViewPager</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://n.sinaimg.cn/sinakd10100/452/w1080h972/20200416/93dc-iskepxs3894040.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">dreamcatcherdeng</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/personal-blog/archives/"><div class="headline">Articles</div><div class="length-num">34</div></a></div><div class="card-info-data-item"><a href="/personal-blog/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a></div><div class="card-info-data-item"><a href="/personal-blog/categories/"><div class="headline">Categories</div><div class="length-num">35</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/DreamcatcherDENG" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%BA%90"><span class="toc-number">1.</span> <span class="toc-text">学习资源</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Java%E7%AE%80%E4%BB%8B"><span class="toc-number">2.</span> <span class="toc-text">Java简介</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#JAVA%E7%8E%AF%E5%A2%83"><span class="toc-number">2.1.</span> <span class="toc-text">JAVA环境</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JAVA%E8%AF%AD%E8%A8%80%E7%89%B9%E7%82%B9"><span class="toc-number">2.2.</span> <span class="toc-text">JAVA语言特点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JAVA%E8%AF%AD%E8%A8%80%E5%8F%91%E5%B1%95%E5%8F%B2"><span class="toc-number">2.3.</span> <span class="toc-text">JAVA语言发展史</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JVM%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B"><span class="toc-number">2.4.</span> <span class="toc-text">JVM内存模型</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA"><span class="toc-number">3.</span> <span class="toc-text">环境搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%93%E4%B8%9A%E6%9C%AF%E8%AF%AD"><span class="toc-number">3.1.</span> <span class="toc-text">专业术语</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BDJDK"><span class="toc-number">3.2.</span> <span class="toc-text">下载JDK</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="toc-number">3.3.</span> <span class="toc-text">环境变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HelloWorld"><span class="toc-number">3.4.</span> <span class="toc-text">HelloWorld</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JShell"><span class="toc-number">3.5.</span> <span class="toc-text">JShell</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95"><span class="toc-number">4.</span> <span class="toc-text">基础语法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E6%B3%A8%E9%87%8A"><span class="toc-number">4.1.</span> <span class="toc-text">文档注释</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="toc-number">4.2.</span> <span class="toc-text">标识符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E9%87%8F"><span class="toc-number">4.3.</span> <span class="toc-text">常量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F"><span class="toc-number">4.4.</span> <span class="toc-text">变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.5.</span> <span class="toc-text">数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.5.1.</span> <span class="toc-text">基本类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.5.2.</span> <span class="toc-text">引用类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B-1"><span class="toc-number">4.5.2.1.</span> <span class="toc-text">引用类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8C%85%E8%A3%85%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.5.2.2.</span> <span class="toc-text">包装类型</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">4.6.</span> <span class="toc-text">关键字</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">4.7.</span> <span class="toc-text">运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="toc-number">4.8.</span> <span class="toc-text">流程控制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF"><span class="toc-number">4.9.</span> <span class="toc-text">循环</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%95%B0%E7%BB%84"><span class="toc-number">5.</span> <span class="toc-text">数组</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="toc-number">5.1.</span> <span class="toc-text">一维数组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="toc-number">5.2.</span> <span class="toc-text">多维数组</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-number">6.</span> <span class="toc-text">面向对象</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6"><span class="toc-number">6.1.</span> <span class="toc-text">访问控制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB"><span class="toc-number">6.2.</span> <span class="toc-text">类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8E%A5%E5%8F%A3"><span class="toc-number">6.3.</span> <span class="toc-text">接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9E%9A%E4%B8%BE"><span class="toc-number">6.4.</span> <span class="toc-text">枚举</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E7%B1%BB"><span class="toc-number">6.5.</span> <span class="toc-text">常用类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#static"><span class="toc-number">6.6.</span> <span class="toc-text">static</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E5%A4%A7%E7%89%B9%E6%80%A7"><span class="toc-number">6.7.</span> <span class="toc-text">四大特性</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B"><span class="toc-number">7.</span> <span class="toc-text">泛型</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B%E7%9A%84%E5%A5%BD%E5%A4%84"><span class="toc-number">7.1.</span> <span class="toc-text">泛型的好处</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B%E7%B1%BB"><span class="toc-number">7.2.</span> <span class="toc-text">泛型类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B%E6%96%B9%E6%B3%95"><span class="toc-number">7.3.</span> <span class="toc-text">泛型方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B%E6%8E%A5%E5%8F%A3"><span class="toc-number">7.4.</span> <span class="toc-text">泛型接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%9B%E5%9E%8B%E9%80%9A%E9%85%8D%E7%AC%A6"><span class="toc-number">7.5.</span> <span class="toc-text">泛型通配符</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Stream-API"><span class="toc-number">8.</span> <span class="toc-text">Stream API</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3"><span class="toc-number">8.1.</span> <span class="toc-text">编程思想</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">8.2.</span> <span class="toc-text">Lambda表达式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%A0%BC%E5%BC%8F"><span class="toc-number">8.2.1.</span> <span class="toc-text">Lambda表达式格式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9C%81%E7%95%A5%E6%A8%A1%E5%BC%8F"><span class="toc-number">8.2.2.</span> <span class="toc-text">Lambda表达式省略模式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E5%BC%95%E7%94%A8"><span class="toc-number">8.3.</span> <span class="toc-text">方法引用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3"><span class="toc-number">8.4.</span> <span class="toc-text">函数式接口</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3"><span class="toc-number">8.4.1.</span> <span class="toc-text">定义函数式接口</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3Function"><span class="toc-number">8.4.2.</span> <span class="toc-text">函数式接口Function</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Stream-API-1"><span class="toc-number">8.5.</span> <span class="toc-text">Stream API</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Stream-API%E6%8F%90%E4%BE%9B%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-number">8.5.1.</span> <span class="toc-text">Stream API提供的方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8"><span class="toc-number">9.</span> <span class="toc-text">异常</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E4%BD%93%E7%B3%BB"><span class="toc-number">9.1.</span> <span class="toc-text">异常体系</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86"><span class="toc-number">9.2.</span> <span class="toc-text">异常处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8"><span class="toc-number">9.3.</span> <span class="toc-text">自定义异常</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%9B%86%E5%90%88"><span class="toc-number">10.</span> <span class="toc-text">集合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%95%E5%88%97%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84Collection"><span class="toc-number">10.1.</span> <span class="toc-text">单列存储结构Collection</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Set"><span class="toc-number">10.1.1.</span> <span class="toc-text">Set</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#List"><span class="toc-number">10.1.2.</span> <span class="toc-text">List</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Queue"><span class="toc-number">10.1.3.</span> <span class="toc-text">Queue</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Stack"><span class="toc-number">10.1.4.</span> <span class="toc-text">Stack</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%8C%E5%88%97%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84Map"><span class="toc-number">10.2.</span> <span class="toc-text">双列存储结构Map</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E9%9B%86%E5%90%88%E7%B1%BB"><span class="toc-number">10.3.</span> <span class="toc-text">常用集合类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Collection%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">10.3.1.</span> <span class="toc-text">Collection常用方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Collection%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-number">10.3.2.</span> <span class="toc-text">Collection的遍历</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#List-1"><span class="toc-number">10.3.2.1.</span> <span class="toc-text">List</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%89%B9%E7%82%B9%EF%BC%9A"><span class="toc-number">10.3.2.1.1.</span> <span class="toc-text">特点：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">10.3.2.1.2.</span> <span class="toc-text">常用方法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Set-1"><span class="toc-number">10.3.2.2.</span> <span class="toc-text">Set</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%89%B9%E7%82%B9"><span class="toc-number">10.3.2.2.1.</span> <span class="toc-text">特点:</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95-1"><span class="toc-number">10.3.2.2.2.</span> <span class="toc-text">常用方法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Queue-1"><span class="toc-number">10.3.2.3.</span> <span class="toc-text">Queue</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%89%B9%E7%82%B9-1"><span class="toc-number">10.3.2.3.1.</span> <span class="toc-text">特点:</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95-2"><span class="toc-number">10.3.2.3.2.</span> <span class="toc-text">常用方法</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Stack-1"><span class="toc-number">10.3.2.4.</span> <span class="toc-text">Stack</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%89%B9%E7%82%B9%EF%BC%9A-1"><span class="toc-number">10.3.2.4.1.</span> <span class="toc-text">特点：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Map%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">10.3.3.</span> <span class="toc-text">Map常用方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Map%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-number">10.3.4.</span> <span class="toc-text">Map的遍历</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#IO%E6%B5%81"><span class="toc-number">11.</span> <span class="toc-text">IO流</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#File"><span class="toc-number">11.1.</span> <span class="toc-text">File</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAFile%E5%AF%B9%E8%B1%A1"><span class="toc-number">11.1.0.1.</span> <span class="toc-text">创建File对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">11.1.0.2.</span> <span class="toc-text">对象的使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#File%E7%B1%BB%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">11.1.0.3.</span> <span class="toc-text">File类常用方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#IO%E6%B5%81%E5%B8%B8%E7%94%A8%E4%BD%93%E7%B3%BB"><span class="toc-number">11.2.</span> <span class="toc-text">IO流常用体系</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81"><span class="toc-number">11.3.</span> <span class="toc-text">字节流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE"><span class="toc-number">11.3.1.</span> <span class="toc-text">字节流写入数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81%E8%AF%BB%E6%95%B0%E6%8D%AE"><span class="toc-number">11.3.2.</span> <span class="toc-text">字节流读数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8B%B7%E8%B4%9D%E6%96%87%E4%BB%B6"><span class="toc-number">11.3.3.</span> <span class="toc-text">拷贝文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%B5%81"><span class="toc-number">11.4.</span> <span class="toc-text">字符流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%B5%81%E8%AF%BB%E6%95%B0%E6%8D%AE"><span class="toc-number">11.4.1.</span> <span class="toc-text">字符流读数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%B5%81%E5%86%99%E6%95%B0%E6%8D%AE"><span class="toc-number">11.4.2.</span> <span class="toc-text">字符流写数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8B%B7%E8%B4%9D%E6%96%87%E6%9C%AC"><span class="toc-number">11.4.3.</span> <span class="toc-text">拷贝文本</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%81%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86"><span class="toc-number">11.5.</span> <span class="toc-text">流异常处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%98%E5%8F%96%E5%AF%B9%E8%B1%A1"><span class="toc-number">11.6.</span> <span class="toc-text">存取对象</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B3%A8%E8%A7%A3"><span class="toc-number">12.</span> <span class="toc-text">注解</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%83%E6%B3%A8%E8%A7%A3"><span class="toc-number">12.1.</span> <span class="toc-text">元注解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%B3%A8%E8%A7%A3"><span class="toc-number">12.2.</span> <span class="toc-text">自定义注解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E6%B3%A8%E8%A7%A3"><span class="toc-number">12.3.</span> <span class="toc-text">内置注解</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%8D%E5%B0%84"><span class="toc-number">13.</span> <span class="toc-text">反射</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%8A%A0%E8%BD%BD"><span class="toc-number">13.1.</span> <span class="toc-text">类加载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%99%A8"><span class="toc-number">13.2.</span> <span class="toc-text">类加载器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JVM%E7%9A%84%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6"><span class="toc-number">13.2.1.</span> <span class="toc-text">JVM的类加载机制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%99%A8-1"><span class="toc-number">13.2.2.</span> <span class="toc-text">类加载器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">13.3.</span> <span class="toc-text">类初始化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%8D%E5%B0%84%E8%8E%B7%E5%8F%96%E7%B1%BB%E7%9A%84%E4%BF%A1%E6%81%AF"><span class="toc-number">13.4.</span> <span class="toc-text">反射获取类的信息</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96Class%E7%B1%BB%E7%9A%84%E5%AF%B9%E8%B1%A1"><span class="toc-number">13.4.1.</span> <span class="toc-text">获取Class类的对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E7%B1%BB%E7%9A%84%E6%88%90%E5%91%98"><span class="toc-number">13.4.2.</span> <span class="toc-text">获取类的成员</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E5%99%A8"><span class="toc-number">13.4.3.</span> <span class="toc-text">获取类的构造器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%88%90%E5%91%98"><span class="toc-number">13.4.4.</span> <span class="toc-text">操作对象的成员</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%8D%E5%B0%84%E4%B8%8E%E6%B3%9B%E5%9E%8B"><span class="toc-number">13.4.5.</span> <span class="toc-text">反射与泛型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%8D%E5%B0%84%E4%B8%8E%E6%B3%A8%E8%A7%A3"><span class="toc-number">13.4.6.</span> <span class="toc-text">反射与注解</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C"><span class="toc-number">14.</span> <span class="toc-text">网络</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#InetAddress%E7%B1%BB"><span class="toc-number">14.1.</span> <span class="toc-text">InetAddress类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#UDP%E9%80%9A%E4%BF%A1%E7%BB%83%E4%B9%A0"><span class="toc-number">14.2.</span> <span class="toc-text">UDP通信练习</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#TCP%E9%80%9A%E4%BF%A1%E7%BB%83%E4%B9%A0"><span class="toc-number">14.3.</span> <span class="toc-text">TCP通信练习</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B9%B6%E5%8F%91"><span class="toc-number">15.</span> <span class="toc-text">并发</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E7%BA%BF%E7%A8%8B"><span class="toc-number">15.1.</span> <span class="toc-text">多线程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E8%BF%9B%E7%A8%8B"><span class="toc-number">15.2.</span> <span class="toc-text">多进程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E7%B1%BB%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E5%AF%B9%E6%AF%94"><span class="toc-number">15.3.</span> <span class="toc-text">常用类线程安全对比</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%97%AE%E9%A2%98%E6%B1%87%E6%80%BB"><span class="toc-number">16.</span> <span class="toc-text">问题汇总</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%85%E8%A7%A3%E5%86%B3%E9%97%AE%E9%A2%98"><span class="toc-number">16.1.</span> <span class="toc-text">待解决问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B7%B2%E8%A7%A3%E5%86%B3%E9%97%AE%E9%A2%98"><span class="toc-number">16.2.</span> <span class="toc-text">已解决问题</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/RegularExpression/" title="正则表达式"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/romance-2258599_960_720.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="正则表达式"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/RegularExpression/" title="正则表达式">正则表达式</a><time datetime="2022-07-13T09:38:01.002Z" title="Created 2022-07-13 17:38:01">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/Nodejs/" title="JavaScript and NodeJs"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-0qxx54.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="JavaScript and NodeJs"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/Nodejs/" title="JavaScript and NodeJs">JavaScript and NodeJs</a><time datetime="2022-07-13T09:38:00.960Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/MySQL/" title="MySQL"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-e7ek7k.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="MySQL"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/MySQL/" title="MySQL">MySQL</a><time datetime="2022-07-13T09:38:00.957Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/JAVA/" title="JAVA"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-72rd8e.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="JAVA"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/JAVA/" title="JAVA">JAVA</a><time datetime="2022-07-13T09:38:00.948Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/personal-blog/2022/07/13/ViewPager/" title="ViewPager"><img src="https://gitee.com/DreamcatcherDENG/drawing-bed/raw/master/img/wallhaven-0qxx54.jpg" onerror="this.onerror=null;this.src='/personal-blog/img/404.jpg'" alt="ViewPager"/></a><div class="content"><a class="title" href="/personal-blog/2022/07/13/ViewPager/" title="ViewPager">ViewPager</a><time datetime="2022-07-13T09:38:00.929Z" title="Created 2022-07-13 17:38:00">2022-07-13</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By dreamcatcherdeng</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Toggle Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">Local search</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  Loading the Database</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/personal-blog/js/utils.js"></script><script src="/personal-blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/personal-blog/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css"><script src="https://cdn.jsdelivr.net/npm/katex@latest/dist/contrib/copy-tex.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/contrib/copy-tex.css"><script>(() => {
  document.querySelectorAll('#article-container span.katex-display').forEach(item => {
    btf.wrap(item, 'div', { class: 'katex-wrap'})
  })
})()</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/personal-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/personal-blog/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":250,"height":300,"hOffset":0,"vOffset":0,"opacity":0.1},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>